member template methods

F

Floogle

How come I get a "error C2062: type 'int' unexpected" error with the
second version?

Is it possible to achieve the same functiuonaility as the fn as a
class method?

Many thanks

//this is ok
template <class T>
T* CreateAnInstanceFn()
{
return new T();
}

//but this isn't
class MyClass
{
public:

template <class T>
T* CreateAnInstance()
{
return new T();
}

};


int main()
{
//legal
int* j = CreateAnInstanceFn<int>();

//uh oh!
MyClass mc;
int* i = mc.CreateAnInstance<int>();

return 0;
}
 
S

Stephane Wirtel

Can you try with typename ?

class MyClass {
public:
template <typename T> T * createAnInstance () {
return new T ();
}
};
 
N

Neelesh Bodas

Stephane said:
Can you try with typename ?

class MyClass {
public:
template <typename T> T * createAnInstance () {
return new T ();
}
};

typename and class are synonyms when used inside <>s after the keyword
"template". So that should not solve the problem (if there was a
problem at first place)

To the OP - The code in your post compiles well on Comeau online. May
be the problem lies with your compiler.
 
N

Neelesh Bodas

Stephane said:
Can you try with typename ?

class MyClass {
public:
template <typename T> T * createAnInstance () {
return new T ();
}
};

typename and class are synonyms when used inside <>s after the keyword
"template". So that should not solve the problem (if there was a
problem at first place)

To the OP - The code in your post compiles well on Comeau online. May
be the problem lies with your compiler.
 
F

Floogle

typename and class are synonyms when used inside <>s after the keyword
"template". So that should not solve the problem (if there was a
problem at first place)

To the OP - The code in your post compiles well on Comeau online. May
be the problem lies with your compiler.


I'm using MSVC6... :eek:(
 
M

mlimber

Floogle said:
I'm using MSVC6... :eek:(

VC6 is known to have many template problems. You can consult the MS
newsgroups for more details, but one solution is to convert the
template type specification to automatic type deduction through
overloading by using this trick from _Modern C++ Design_:

template<typename T>
struct Type2Type { typedef T Type; };

class MyClass
{
public:
template <class T>
T* CreateAnInstance( Type2Type<T> )
{
return new T();
}
};

int main()
{
MyClass mc;
int* i = mc.CreateAnInstance( Type2Type<int>() );
return 0;
}

You might also make MyClass::CreateAnInstance() either static or const,
depending on what your real code does. Also, you might consider using
returning a smart pointer (e.g., std::auto_ptr) rather than a raw
pointer to help prevent leaks:

#include <memory>
using namespace std;

class MyClass
{
public:
template <class T>
static auto_ptr<T> CreateAnInstance( Type2Type<T> )
{
return auto_ptr<T>( new T() );
}
};

Cheers! --M
 
V

Victor Bazarov

Floogle said:
[..]
To the OP - The code in your post compiles well on Comeau online. May
be the problem lies with your compiler.



I'm using MSVC6... :eek:(

Upgrade. VC++ v6 is notoriously bad with member templates. Or fall
back onto your work-around (by keeping your function non-member).

V
 

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
474,202
Messages
2,571,057
Members
47,660
Latest member
vidotip479

Latest Threads

Top