473,377 Members | 1,119 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

strange template problem.

Hi.all
look coding as below please:
class A
{
public:
template<class Tcreate(){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.

Aug 16 '06 #1
11 1291

Gary li wrote:
Hi.all
look coding as below please:
class A
{
public:
template<class Tcreate(){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,

Aug 16 '06 #2
"Gary li" <li*************@sina.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com
Hi.all
look coding as below please:
class A
{
public:
template<class Tcreate(){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.

--
John Carson


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

Gary li wrote:
Hi.all
look coding as below please:
class A
{
public:
template<class Tcreate(){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.
Aug 16 '06 #4
John Carson wrote:
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 TT* create(T* dummy = 0) { ... }
Aug 16 '06 #5
"Ron Natalie" <ro*@spamcop.netwrote in message
news:44***********************@news.newshosting.co m
John Carson wrote:
>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 TT* 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);

--
John Carson
Aug 16 '06 #6
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?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 16 '06 #7
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:eb**********@news.datemas.de
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.

--
John Carson
Aug 16 '06 #8
John Carson wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:eb**********@news.datemas.de
>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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 16 '06 #9
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:eb**********@news.datemas.de
John Carson wrote:
>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:eb**********@news.datemas.de
>>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.

--
John Carson
Aug 16 '06 #10

Rickfjord wrote:
Gary li wrote:
Hi.all
look coding as below please:
class A
{
public:
template<class Tcreate(){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;"?
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

Aug 16 '06 #11
John Carson wrote:
[..]
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.
Aug 16 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: muser | last post by:
I have written a function that checks the first four characters in an address are valid; i.e. 1d2 sour st would prove to be invalid. bool checkdigitsinaddress( const char* string ) { for( int...
4
by: Tjerk Wolterink | last post by:
I've xml code like this: roles.xml: <?xml version="1.0" encoding="ISO-8859-1"?> <roles xmlns="http://www.wolterinkwebdesign.com/xml/roles"> <!-- ! The admin role. ! And admin should have...
2
by: Jason Heyes | last post by:
The following program does not compile. Apparantly "t" is inaccessible. #include <iostream> using namespace std; template <class T> class Foo { T t; public: Foo(T t_) : t(t_) { }
2
by: Gandu | last post by:
Could some C++ guru shed some light as to what might be going on? I have the following two classes which are simply data containers: const unsigned int MAXSIZE = 40; class DataItem{ char...
5
by: sriram | last post by:
I have the following: class BSA { ... ... ... ... public: enum VRGAttrId {
3
by: rob.guitar.rob | last post by:
Hello, My last few posts have been revolving aroung the same problem, and I still cant solve it and I would be really appreciate if anyone could spot a problem. a section of my XML goes like...
4
by: Nate Murray | last post by:
Hey all, I'm having a strange PHP (4.3.10) problem that seems to have something to do with javascript. This is a bit complex, so hold on to your hats. My issue is that a single function is...
1
by: stromhau | last post by:
Ok, i have a file with main and an additional .cpp file i include in the main file but i get a lot of strange warnings when including. Both files compile just great separately. It seems that it have...
3
by: Zheng Da | last post by:
Hello, I want a use the class of map in a class with template, and the type of elements in map is specified by the parameter of the class's template. For example, template <typename key,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.