strange template problem.

G

Gary li

Hi.all
look coding as below please:
class A
{
public:
template<class T> create(){return new T;}
};
int main()
{
A a;
int *n = a.create<int>();
delete n;
return 0;
};
compiling is fail with vc6 but successful with g++ under linux.
 
R

Rickfjord

Gary said:
Hi.all
look coding as below please:
class A
{
public:
template<class T> create(){return new T;}
};
int main()
{
A a;
int *n = a.create<int>();
delete n;
return 0;
};
compiling is fail with vc6 but successful with g++ under linux.

Hi Gary,

Did you declare "using namespace std;"? Otherwise the compiler might
not know where to find the template class. This might be handled
differently in G++ than in Microsoft's compiler. This would explain why
the results are different using different compilers.

Hope this helps,
 
J

John Carson

Gary li said:
Hi.all
look coding as below please:
class A
{
public:
template<class T> create(){return new T;}
};
int main()
{
A a;
int *n = a.create<int>();
delete n;
return 0;
};
compiling is fail with vc6 but successful with g++ under linux.

You haven't specified a return type for your create() function, so it should
not compile anywhere.

Perhaps the above code is not your actual code. Always copy and paste into
your post, never retype since retyping has a high risk of error.

Adding a return type of T* and removing the spurious ; after main() will get
the code to compile on any Standard compliant compiler. It won't compile on
VC++ 6 because VC++ 6 has major compliance problems in the area of
templates.
 
G

Gary li

If I move template function out of class it can compile ok.
template<class T> T* create(){return new T;}
int main()
{
int * n = create<int>();
printf("%d\n", *n);
delete n;
return 0;
}
 
R

Ron Natalie

John said:
Adding a return type of T* and removing the spurious ; after main() will get
the code to compile on any Standard compliant compiler. It won't compile on
VC++ 6 because VC++ 6 has major compliance problems in the area of
templates.
Specifically in this case there is a bug in Visual Studio's
implementation when it comes to function definitions that
do not have some variant of the template variable in their
signature (and hence the decoration) and it substitutes the
same instantiation of the template for all cases. The VC6
workaround is to add a dummy arg:

template<class T> T* create(T* dummy = 0) { ... }
 
J

John Carson

Ron Natalie said:
Specifically in this case there is a bug in Visual Studio's
implementation when it comes to function definitions that
do not have some variant of the template variable in their
signature (and hence the decoration) and it substitutes the
same instantiation of the template for all cases. The VC6
workaround is to add a dummy arg:

template<class T> T* create(T* dummy = 0) { ... }

That still won't compile.

This will, but it is a pain in the neck:

class A
{
public:
template<class T>
T* create(T* dummy){return new T;}
};

int main()
{
A a;
int *n = a.create((int*)0);
delete n;
return 0;
}

Note that it won't compile if you change

int *n = a.create((int*)0);

to

int *n = a.create<int>((int*)0);
 
V

Victor Bazarov

John said:
[..]
class A
{
public:
template<class T>
T* create(T* dummy){return new T;}
};

int main()
{
A a;
int *n = a.create((int*)0);
delete n;
return 0;
}

Note that it won't compile if you change

int *n = a.create((int*)0);

to

int *n = a.create<int>((int*)0);

Really? It won't? It does, though. When will it stop?

V
 
J

John Carson

Victor Bazarov said:
John said:
[..]
class A
{
public:
template<class T>
T* create(T* dummy){return new T;}
};

int main()
{
A a;
int *n = a.create((int*)0);
delete n;
return 0;
}

Note that it won't compile if you change

int *n = a.create((int*)0);

to

int *n = a.create<int>((int*)0);

Really? It won't? It does, though. When will it stop?

It doesn't compile for me. Remembering our last disagreement on whether
something would compile, I have also tried disabling language extensions,
but it makes no difference.
 
V

Victor Bazarov

John said:
Victor Bazarov said:
John said:
[..]
class A
{
public:
template<class T>
T* create(T* dummy){return new T;}
};

int main()
{
A a;
int *n = a.create((int*)0);
delete n;
return 0;
}

Note that it won't compile if you change

int *n = a.create((int*)0);

to

int *n = a.create<int>((int*)0);

Really? It won't? It does, though. When will it stop?

It doesn't compile for me. Remembering our last disagreement on
whether something would compile, I have also tried disabling language
extensions, but it makes no difference.

What compiler? Comeau online accepts it fine.

V
 
J

John Carson

Victor Bazarov said:
John said:
Victor Bazarov said:
John Carson wrote:
[..]
class A
{
public:
template<class T>
T* create(T* dummy){return new T;}
};

int main()
{
A a;
int *n = a.create((int*)0);
delete n;
return 0;
}

Note that it won't compile if you change

int *n = a.create((int*)0);

to

int *n = a.create<int>((int*)0);

Really? It won't? It does, though. When will it stop?

It doesn't compile for me. Remembering our last disagreement on
whether something would compile, I have also tried disabling language
extensions, but it makes no difference.

What compiler? Comeau online accepts it fine.


Refer to the post to which you first replied. We are talking about VC++ 6.
 
T

Thomas Tutone

Rickfjord said:
Gary li wrote:

Hi Gary,

Did you declare "using namespace std;"?

The example program uses nothing from namespace std, so including that
is both unnecessary and a bad habit to get into.
Otherwise the compiler might
not know where to find the template class.

Not true. The template class isn't in namespace std.

Best regards,

Tom
 
V

Victor Bazarov

John said:
[..]
Refer to the post to which you first replied. We are talking about
VC++ 6.

Oh... Truly sorry about that. No, seriously, I am truly sorry for
anybody who has to use such an old and crappy compiler.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top