in this question:
template; Point <2, double>; Point <3, double>
Dennis and Michael spotted an unreasonably stupidly implemented constructor.
They were right, I did not consider it at that moment. But I found out that the constructor doesnโt help much for a template class like this, instead the function here is much more convenient and safer
namespace point { template < unsigned int dims, typename T > struct Point { TX[ dims ]; std::string str() { std::stringstream s; s << "{"; for ( int i = 0; i < dims; ++i ) { s << " X" << i << ": " << X[ i ] << (( i < dims -1 )? " |": " "); } s << "}"; return s.str(); } Point<dims, int> toint() { Point<dims, int> ret; std::copy( X, X+dims, ret.X ); return ret; } }; template < typename T > Point< 2, T > Create( T X0, T X1 ) { Point< 2, T > ret; ret.X[ 0 ] = X0; ret.X[ 1 ] = X1; return ret; } template < typename T > Point< 3, T > Create( T X0, T X1, T X2 ) { Point< 3, T > ret; ret.X[ 0 ] = X0; ret.X[ 1 ] = X1; ret.X[ 2 ] = X2; return ret; } template < typename T > Point< 4, T > Create( T X0, T X1, T X2, T X3 ) { Point< 4, T > ret; ret.X[ 0 ] = X0; ret.X[ 1 ] = X1; ret.X[ 2 ] = X2; ret.X[ 3 ] = X3; return ret; } }; int main( void ) { using namespace point; Point< 2, double > p2d = point::Create( 12.3, 34.5 ); Point< 3, double > p3d = point::Create( 12.3, 34.5, 56.7 ); Point< 4, double > p4d = point::Create( 12.3, 34.5, 56.7, 78.9 );
Does the new C ++ standard have any new improvements, language features or simplifications regarding this aspect of the ctor template class?
what do you think about implementing a combination of namespace, stuct, and create?
thank you very much in advance
Unfortunately,
c ++ function constructor templates
OlimilOops May 08 '10 at 2:09 p.m. 2010-05-08 14:09
source share