Is there a way to use the Idiom Named Constructor with templates in a “pretty” way?
For instance:
#include <vector>
using namespace std;
template< typename T >
class Foo
{
public:
static Foo Copy(const T& arg)
{
Foo ret;
ret.t_copy = arg;
return ret;
}
static Foo CopyClear(const T& arg)
{
Foo ret;
ret.t_copy = arg;
ret.t_copy.clear();
return ret;
}
private:
T t_copy;
};
int main( int argc, char** argv )
{
vector<double> vec;
vec.push_back(1);
Foo< vector<double> > a_foo = Foo::CopyClear( vec );
Foo< vector<double> > a_foo = Foo< vector<double> >::CopyClear( vec );
return 0;
}
I would like to use the syntax somehow #1. #2works, but rubs my DRY in the wrong way.
EDIT: A new, more "realistic" version Foo.
EDIT2: No C ++ 0x / C ++ 1x for me, I'm afraid :(
source
share