can constructor be a template?

D

Deep

Can I use a class in this manner, where a constructor is of templated:

template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}
};

please guide me...


regards,
john
 
A

Alf P. Steinbach

* Deep:
Can I use a class in this manner, where a constructor is of templated:

template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}
};

please guide me...

You can use templated constructors, but a templated constructor is never
formally a copy constructor. Therefore, the compiler may generate a
copy constructor, and use that, if copying of same type is invoked.
This is difficult to test because the compiler is generally allowed to
remove copy constructor calls even if the copy constructor has some side
effect.

Also, you need some other constructor in order to create any object in
the first place.

You can try (although output from the copy constructor is not guaranteed)

#include <iostream>
#include <ostream>

void say( char const s[] ) { std::cout << s << std::endl; }

template<typename T>
class MyClass1
{
public:
int x_;
public:
MyClass1(): x_( 0 ) {}
template<typename U>
MyClass1( MyClass1<U> const& other ): x_( other.x_ )
{ say( "Template constructor 1." ); }
};

template<typename T>
class MyClass2
{
public:
int x_;
public:
MyClass2(): x_( 0 ) {}
template<typename U>
MyClass2( MyClass2<U> const& other ): x_( other.x_ )
{ say( "Template constructor 2." ); }
MyClass2( MyClass2 const& other ): x_( other.x_ )
{ say( "Copy constructor 2." ); }
};

typedef MyClass1<int> Class1;
typedef MyClass2<int> Class2;

void foo( Class1 ) { say( "foo 1" ); }
void foo( Class2 ) { say( "foo 2" ); }

int main()
{
Class1 o1;
foo( o1 );
Class2 o2;
foo( o2 );
}
 
R

rtk.phill

Can I use a class in this manner, where a constructor is of templated:

template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}

};

please guide me...

regards,
john

Not sure if this will work, but it requires prior knowledge of what
"T" actually is, so you specify a constructor for each possible type
of "T";

for example;

public:
my_class(const my_class<int>&other) : x_(other.x_) {}
my_class(const my_class<char*>&other) : x_(other.x_) { }

I hope I've understood the question correctly!
 
A

Alf P. Steinbach

* Alf P. Steinbach:
You can try (although output from the copy constructor is not guaranteed)

Oh dang. In this specific case (function argument by value) as far as I
recall the copy constructor call can't be elided by the compiler.
Sorry, the rules are special-cased, so I just rely on the general rule
"Don't ever rely on side effects in copy constructors", simpler.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Can I use a class in this manner, where a constructor is of templated:

template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}
};

please guide me...

Sure you can:

#include <iostream>

template<class T>
class Test
{
public:
T x_;
Test(T t) :x_(t) {}

template<class U>
Test(Test<U> t) : x_(t.x_) { }
T get() { return x_; }
};

int main()
{
Test<int> t1(1);
Test<double> t2(t1);
std::cout << t2.get();
}

Notice however that this requires that x_ is public, or else the
constructor will not have access to t.x_ above unless T == U (since the
two instantiations of Test would then have different types and no access
to the other types private data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top