template probleme

Y

yomgui

Hi,

The following code compiles on Unix but refuse to do so with devstudio6

can anyone help me to get it right ? (ie for visual studio)


template <class T> class AutoPtr
{
public:
/** Default constructor. */
AutoPtr(T* p = 0);

/** Copy contructing an AutoPtr from another AutoPtr causes the old
one
* to be released from destruction duty. */
AutoPtr(const AutoPtr &a);

/** Copy contructing an AutoPtr from a pointer with a different element
type. */
template<class T1> AutoPtr(const AutoPtr<T1> &a);
}

devstudio says: member function already defined or declared
on the last constructor

thanks

yomgui
 
D

Donovan Rebbechi

Hi,

The following code compiles on Unix but refuse to do so with devstudio6

can anyone help me to get it right ? (ie for visual studio)


template <class T> class AutoPtr
{
public:
/** Default constructor. */
AutoPtr(T* p = 0);

/** Copy contructing an AutoPtr from another AutoPtr causes the old
one
* to be released from destruction duty. */
AutoPtr(const AutoPtr &a);

/** Copy contructing an AutoPtr from a pointer with a different element
type. */
template<class T1> AutoPtr(const AutoPtr<T1> &a);
}

devstudio says: member function already defined or declared
on the last constructor

Looks like an issue with the compiler -- the parameterized constructor is
*not* a copy constructor.

Here's a question for you -- what is the output of this code on your compiler ?
The template constructor should not be called on copy -- the compiler should
still create one.

/*
template construct ...
conversion construct called!
calling copy construct ...
*/

#include <iostream>

class X {
public:

template <typename T> X(const T& ) {
std::cout << "conversion construct called!" << std::endl;
}
};


int main()
{
std::cout << "template construct ... " << std::endl;
X x(3);
std::cout << "calling copy construct ... " << std::endl;
X y(x);
}


Cheers,
 
V

Victor Bazarov

yomgui said:
The following code compiles on Unix but refuse to do so with devstudio6

can anyone help me to get it right ? (ie for visual studio)

You will have to upgrade your compiler to 7.1 or 8.0 Beta in order to
make it work. VC++ v6 is notoriously bad with member templates.
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top