K
Kai-Uwe Bux
Hi folks,
I observed something that puzzles me. When I do
namespace xxx { using std::swap; }
it appears that xxx::swap and std::swap are not strictly equivalent. In
particular, I think that my implementation will only choose the partial
specialization for std::vector when std::swap is used.
Consider the following code (ignore the part implementing the timing):
#include <ctime>
#include <iostream>
#include <utility>
#include <string>
class timer {
private:
std::clock_t ticks;
public:
timer ( void )
: ticks ( std::clock() )
{}
std::clock_t passed ( void ) const {
return( std::clock() - ticks );
}
double seconds ( void ) const {
return( double( this->passed() ) / CLOCKS_PER_SEC );
}
}; // timer
class print_timer {
private:
std:stream & o_str;
std::string banner;
timer watch;
public:
print_timer ( std:stream & str, std::string msg = std::string() )
: o_str ( str )
, banner ( msg )
, watch ()
{}
~print_timer ( void ) {
o_str << banner << " " << watch.seconds() << "sec" << '\n';
}
}; // print_timer
#define PRINT_TIME print_timer print_timer_dummy
namespace xxx { using std::swap; }
#include <vector>
typedef std::vector< long unsigned > IntVector;
int main ( void ) {
{
IntVector a ( 20000000, 20000000 );
IntVector b ( 20000000, 20000000 );
{
PRINT_TIME( std::cout, "xxx::swap: " );
xxx::swap( a, b );
}
{
PRINT_TIME( std::cout, "std::swap: " );
std::swap( a, b );
}
{
PRINT_TIME( std::cout, "std::swap: " );
std::swap( a, b );
}
{
PRINT_TIME( std::cout, "xxx::swap: " );
xxx::swap( a, b );
}
{
PRINT_TIME( std::cout, "std::swap: " );
std::swap( a, b );
}
}
}
On my machine, this prints:
news_group> a.out
xxx::swap: 0.31sec
std::swap: 0sec
std::swap: 0sec
xxx::swap: 0.33sec
std::swap: 0sec
The different times strongly indicate that std::swap uses a partial
specialization for vectors whereas xxx::swap does not. Is that standard? If
so, why?
Best
Kai-Uwe Bux
I observed something that puzzles me. When I do
namespace xxx { using std::swap; }
it appears that xxx::swap and std::swap are not strictly equivalent. In
particular, I think that my implementation will only choose the partial
specialization for std::vector when std::swap is used.
Consider the following code (ignore the part implementing the timing):
#include <ctime>
#include <iostream>
#include <utility>
#include <string>
class timer {
private:
std::clock_t ticks;
public:
timer ( void )
: ticks ( std::clock() )
{}
std::clock_t passed ( void ) const {
return( std::clock() - ticks );
}
double seconds ( void ) const {
return( double( this->passed() ) / CLOCKS_PER_SEC );
}
}; // timer
class print_timer {
private:
std:stream & o_str;
std::string banner;
timer watch;
public:
print_timer ( std:stream & str, std::string msg = std::string() )
: o_str ( str )
, banner ( msg )
, watch ()
{}
~print_timer ( void ) {
o_str << banner << " " << watch.seconds() << "sec" << '\n';
}
}; // print_timer
#define PRINT_TIME print_timer print_timer_dummy
namespace xxx { using std::swap; }
#include <vector>
typedef std::vector< long unsigned > IntVector;
int main ( void ) {
{
IntVector a ( 20000000, 20000000 );
IntVector b ( 20000000, 20000000 );
{
PRINT_TIME( std::cout, "xxx::swap: " );
xxx::swap( a, b );
}
{
PRINT_TIME( std::cout, "std::swap: " );
std::swap( a, b );
}
{
PRINT_TIME( std::cout, "std::swap: " );
std::swap( a, b );
}
{
PRINT_TIME( std::cout, "xxx::swap: " );
xxx::swap( a, b );
}
{
PRINT_TIME( std::cout, "std::swap: " );
std::swap( a, b );
}
}
}
On my machine, this prints:
news_group> a.out
xxx::swap: 0.31sec
std::swap: 0sec
std::swap: 0sec
xxx::swap: 0.33sec
std::swap: 0sec
The different times strongly indicate that std::swap uses a partial
specialization for vectors whereas xxx::swap does not. Is that standard? If
so, why?
Best
Kai-Uwe Bux