Sign in to follow this  
Erzengeldeslichtes

Is this standard template behavior?

Recommended Posts

I don't have the C++ standard nor the dough to shell out for a copy of it, so I ask those of you who have it: Is this standard behaviour?
template<typename T>
class MyTemplatedClass
{
public:
	template<typename P>
	MyTemplatedClass(const MyTemplatedClass<P>& rs)
	{
		__asm { int 3; }
	}
};
void main()
{
	MyTemplatedClass<int> i;
	MyTemplatedClass<int> b = i;//Doesn't call templated CTOR
	MyTemplatedClass<double> c = i;//Calls templated CTOR just fine
}

Now this is with MSVC 2005 Express Edition. I expected it to call the templated constructor since P is T. Does the standard say differently?

Share this post


Link to post
Share on other sites
SiCrane    11839
If you don't define a copy constructor yourself the compiler will define one for you. And it doesn't accept the templated constructor as a copy constructor, you'll have to define one explicitly that does the behaviour you specify.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
If you don't define a copy constructor yourself the compiler will define one for you. And it doesn't accept the templated constructor as a copy constructor, you'll have to define one explicitly that does the behaviour you specify.


That's what I thought was happening, but I was wondering if that was standard or vender specific as to wether the template could be considered a copy constructor.

Share this post


Link to post
Share on other sites
Sonal    122
This is the normal response:
Template copy constructor is NOT same as normal copy constructor and since u didnt define a normral one (in this case with <typename T>) the compiler make one for you.
See 12.8 in the C++ standard.

Sharon

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this