Quote:Quote:Original post by DevFred
Quote:Original post by visitor
I don't see why all the fuss.
The problem is ...
Oooh, I didn't expect someone to actually have a legitimate answer to my earlier loufoque-targeted question [lol]. I'll have to write a CREATE_STD_SWAP_SPECIALIZATIONS macro using BOOST_PP I guess :P
#include <boost/preprocessor.hpp>#include <iostream>#include <algorithm>#define INDUSTRY_CSSS_NAME( product ) BOOST_PP_SEQ_HEAD(product) < BOOST_PP_SEQ_ENUM( BOOST_PP_SEQ_TAIL(product) ) >#define INDUSTRY_CSSS_PROD( r, product ) template<> void ::std::swap< INDUSTRY_CSSS_NAME(product) > \ ( INDUSTRY_CSSS_NAME(product)& lhs, INDUSTRY_CSSS_NAME(product)& rhs ) { lhs.swap(rhs); }#define INDUSTRY_CSSS_SPECIALIZATION( tempname, args ) BOOST_PP_SEQ_FOR_EACH_PRODUCT( INDUSTRY_CSSS_PROD, BOOST_PP_SEQ_INSERT( args, 0, (tempname) ) )#define INDUSTRY_CREATE_STD_SWAP_SPECIALIZATIONS( templatename, args ) INDUSTRY_CSSS_SPECIALIZATION( templatename, args )template < typename T, size_t N > struct Matrix { void swap( Matrix& other ) { std::cout << "custom swap called!\n"; } };INDUSTRY_CREATE_STD_SWAP_SPECIALIZATIONS( ::Matrix, ((signed char)(short)(int)(float)(double)(long double)) ((2)(3)(4)) )int main() { Matrix<float,2> a, b; std::swap( a, b );}
(Mind the space trailing the \ to prevent manglement by [source] tags if copypastaing)
I'm not sure how standard explicitly spelling out the FQN of ::std::swap is when creating the specializations, but it seems to work well on MSVC2k8:
/* no namespace */ INDUSTRY_CREATE_STD_SWAP_SPECIALIZATIONS( ::Matrix, ((signed char)(short)(int)(float)(double)(long double)) ((2)(3)(4)) )Works fine, calls custom swap
namespace std { INDUSTRY_CREATE_STD_SWAP_SPECIALIZATIONS( ::Matrix, ((signed char)(short)(int)(float)(double)(long double)) ((2)(3)(4)) ) }Works fine, calls custom swap
namespace fnord { INDUSTRY_CREATE_STD_SWAP_SPECIALIZATIONS( ::Matrix, ((signed char)(short)(int)(float)(double)(long double)) ((2)(3)(4)) ) }Nice compiler errors instead of, say, silently not specializing the right function:
1>i:\home\projects\test\test\main.cpp(11) : error C2888: 'void std::swap<Matrix<T,N>>(Matrix<T,N> &,Matrix<T,N> &)' : symbol cannot be defined within namespace 'fnord'
1> with
1> [
1> T=signed char,
1> N=2
1> ]
1>i:\home\projects\test\test\main.cpp(11) : error C2888: 'void std::swap<Matrix<T,N>>(Matrix<T,N> &,Matrix<T,N> &)' : symbol cannot be defined within namespace 'fnord'
1> with
1> [
1> T=signed char,
1> N=3
1> ]
...