473,322 Members | 1,259 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,322 software developers and data experts.

possible compiler bug?

I have a template class that instantiates a variable of the template
parameter type as follows:

template <class T>
struct TemplateClass
{
T t;
};

struct TestClass
{
int x;
static TemplateClass<TestClass> v2;
};

Under Visual C++ 7 I get the following compiler error:
error C2079: 'TemplateClass<T>::t' uses undefined struct 'TestClass' with
[ T=TestClass ]

However, it compiles fine under the Borland compiler.

Is this a bug in the Visual C++ compiler?

I would appreciate any comments.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
13 1986
Eric wrote:
I have a template class
that instantiates a variable of the template parameter type as follows:
[snip]
cat recursive.cc #include <iostream>

template <class T>
struct TemplateClass {
T t;
};

struct TestClass {
int x;
static TemplateClass<TestClass> v2;
TestClass(int i = 0): x(i) { }
};

int main(int argc, char* argv[]) {
TestClass t;
std::cout << "t.x = " << t.x << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o recursive recursive.cc
./recursive t.x = 0
Under Visual C++ 7 I get the following compiler error:
error C2079: 'TemplateClass<T>::t' uses undefined struct 'TestClass' with
[ T=TestClass ]

However, it compiles fine under the Borland compiler.

Is this a bug in the Visual C++ compiler?

I would appreciate any comments.

Jul 22 '05 #2
Eric wrote:
I have a template class that instantiates a variable of the template
parameter type as follows:

template <class T>
struct TemplateClass
{
T t;
};

struct TestClass
{
int x;
static TemplateClass<TestClass> v2;
};

Under Visual C++ 7 I get the following compiler error:
error C2079: 'TemplateClass<T>::t' uses undefined struct 'TestClass' with
[ T=TestClass ]

However, it compiles fine under the Borland compiler.

Is this a bug in the Visual C++ compiler?


Quite possible. Try it with VC++ v 7.1.

V
Jul 22 '05 #3


Victor Bazarov wrote:
Eric wrote:
I have a template class that instantiates a variable of the template
parameter type as follows:

template <class T>
struct TemplateClass
{
T t;
};

struct TestClass
{
int x;
static TemplateClass<TestClass> v2;
};

Under Visual C++ 7 I get the following compiler error:
error C2079: 'TemplateClass<T>::t' uses undefined struct 'TestClass' with
[ T=TestClass ]

However, it compiles fine under the Borland compiler.

Is this a bug in the Visual C++ compiler?


Quite possible. Try it with VC++ v 7.1.

V


That definitely does compile with the borland compiler, but I am wondering what
the rules are for this since
TestClass isn't fully defined when the template is instantiated? Same thing
doesn't work without the 'static' - but doing the equivalent without a template
doesn't work at all.

David
Jul 22 '05 #4

"David Lindauer" <ca*****@bluegrass.net> wrote in message
news:41***************@bluegrass.net...


Victor Bazarov wrote:
Eric wrote:
I have a template class that instantiates a variable of the template
parameter type as follows:

template <class T>
struct TemplateClass
{
T t;
};

struct TestClass
{
int x;
static TemplateClass<TestClass> v2;
};

Under Visual C++ 7 I get the following compiler error:
error C2079: 'TemplateClass<T>::t' uses undefined struct 'TestClass' with [ T=TestClass ]

However, it compiles fine under the Borland compiler.

Is this a bug in the Visual C++ compiler?
Quite possible. Try it with VC++ v 7.1.

V


That definitely does compile with the borland compiler, but I am wondering

what the rules are for this since
TestClass isn't fully defined when the template is instantiated? Same thing doesn't work without the 'static' - but doing the equivalent without a template doesn't work at all.

David


TestClass is fully defined when TemplateClass is instantiated - static
variables are not contained in objects and thus do not influence object
size.
This is why I would expect it to compile.
Jul 22 '05 #5
Eric wrote:
I have a template class that instantiates a variable of the template
parameter type as follows:

template <class T>
struct TemplateClass
{
T t;
};

struct TestClass
{
int x;
static TemplateClass<TestClass> v2;
};

Under Visual C++ 7 I get the following compiler error:
error C2079: 'TemplateClass<T>::t' uses undefined struct 'TestClass' with
[ T=TestClass ]

However, it compiles fine under the Borland compiler.

Is this a bug in the Visual C++ compiler?

I would appreciate any comments.


G++ (3.4.3) takes it as well. The circular references between the
template instantiation and its surrounding class is causing some
confusion.
--
A. Kanawati
NO*************@comcast.net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 22 '05 #6
David Lindauer <ca*****@bluegrass.net> wrote i
Eric wrote:
I have a template class that instantiates a variable of the template
parameter type as follows:

template <class T>
struct TemplateClass
{
T t;
};

struct TestClass
{
int x;
static TemplateClass<TestClass> v2;
};

That definitely does compile with the borland compiler, but I am
wondering what the rules are for this since TestClass isn't
fully defined when the template is instantiated? Same thing
doesn't work without the 'static' - but doing the equivalent without
a template doesn't work at all.


Good question...there are a number of slightly tricky things in play
here.

First, you may or may not have noticed that TestClass::v2 isn't
defined. That means that if you write a program that tries to access
TestClass::v2, you'll get a link error. (The *compiler* won't
complain, because TestClass::v2 might be defined in some other
translation unit.)

Next, you probably want to know the point of instantiation of
TemplateClass<TestClass>. There's no explicit instantiation, so the
rules for implicit instantiation apply (14.7.1). In particular, "the
class template specialization is implicitly instantiated when the
specialization is referenced in a context that requires a
completely-defined object type or when the completeness of the class
affects the semantics of the program."

9.4.2/2 says that a static data member may be declared with an
incomplete type, so the declaration, by itself, does not cause the
instantiation of TemplateClass<TestCase>.

Of course, the *definition* of TestClass::v2 *will* require a complete
type, so that may be the point of instatiation. (It will be, unless
something else forces the instantiation first.)

Naturally, the definition of TestClass::v2 will follow the full
declaration of TestClass, so TestClass will be complete, and there
will be no difficulty instantiating TemplateClass<TestClass>, even
though it contains a non-static data member of type TestClass.

Unless I'm wrong.... :-)
Jul 22 '05 #7


johnchx wrote:
David Lindauer <ca*****@bluegrass.net> wrote i
Eric wrote:
> I have a template class that instantiates a variable of the template
> parameter type as follows:
>
> template <class T>
> struct TemplateClass
> {
> T t;
> };
>
> struct TestClass
> {
> int x;
> static TemplateClass<TestClass> v2;
> };
>

That definitely does compile with the borland compiler, but I am
wondering what the rules are for this since TestClass isn't
fully defined when the template is instantiated? Same thing
doesn't work without the 'static' - but doing the equivalent without
a template doesn't work at all.


Good question...there are a number of slightly tricky things in play
here.

First, you may or may not have noticed that TestClass::v2 isn't
defined. That means that if you write a program that tries to access
TestClass::v2, you'll get a link error. (The *compiler* won't
complain, because TestClass::v2 might be defined in some other
translation unit.)

Next, you probably want to know the point of instantiation of
TemplateClass<TestClass>. There's no explicit instantiation, so the
rules for implicit instantiation apply (14.7.1). In particular, "the
class template specialization is implicitly instantiated when the
specialization is referenced in a context that requires a
completely-defined object type or when the completeness of the class
affects the semantics of the program."

9.4.2/2 says that a static data member may be declared with an
incomplete type, so the declaration, by itself, does not cause the
instantiation of TemplateClass<TestCase>.

Of course, the *definition* of TestClass::v2 *will* require a complete
type, so that may be the point of instatiation. (It will be, unless
something else forces the instantiation first.)

Naturally, the definition of TestClass::v2 will follow the full
declaration of TestClass, so TestClass will be complete, and there
will be no difficulty instantiating TemplateClass<TestClass>, even
though it contains a non-static data member of type TestClass.

Unless I'm wrong.... :-)


that makes sense, thanks!

David
Jul 22 '05 #8


Eric wrote:
"David Lindauer" <ca*****@bluegrass.net> wrote in message
news:41***************@bluegrass.net...


Victor Bazarov wrote:
Eric wrote:
> I have a template class that instantiates a variable of the template
> parameter type as follows:
>
> template <class T>
> struct TemplateClass
> {
> T t;
> };
>
> struct TestClass
> {
> int x;
> static TemplateClass<TestClass> v2;
> };
>
> Under Visual C++ 7 I get the following compiler error:
> error C2079: 'TemplateClass<T>::t' uses undefined struct 'TestClass' with > [ T=TestClass ]
>
> However, it compiles fine under the Borland compiler.
>
> Is this a bug in the Visual C++ compiler?

Quite possible. Try it with VC++ v 7.1.

V


That definitely does compile with the borland compiler, but I am wondering

what
the rules are for this since
TestClass isn't fully defined when the template is instantiated? Same

thing
doesn't work without the 'static' - but doing the equivalent without a

template
doesn't work at all.

David


TestClass is fully defined when TemplateClass is instantiated - static
variables are not contained in objects and thus do not influence object
size.
This is why I would expect it to compile.


Thanks!

David
Jul 22 '05 #9
Antoun Kanawati wrote:
Eric wrote:
I have a template class that instantiates a variable of the template
parameter type as follows:

template <class T>
struct TemplateClass
{
T t;
};

struct TestClass
{
int x;
static TemplateClass<TestClass> v2;
};

Under Visual C++ 7 I get the following compiler error:
error C2079: 'TemplateClass<T>::t' uses undefined struct 'TestClass' with
[ T=TestClass ]

However, it compiles fine under the Borland compiler.

Is this a bug in the Visual C++ compiler?

I would appreciate any comments.

G++ (3.4.3) takes it as well. The circular references between the
template instantiation and its surrounding class is causing some
confusion.


It's no confusion. The code is illegal. According to §9 para 2, "a class
is considered defined after the closing brace of its class-specifier."
Therefore at the point of declaration of v2, TestClass has not been
defined yet, so the template TemplateClass<TestClass> cannot be
instantiated because it requires T to be a complete type.

Alberto

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #10
Alberto Barbati <Al************@libero.it> wrote in message news:<nD********************@twister2.libero.it>.. .
Antoun Kanawati wrote:
Eric wrote:
I have a template class that instantiates a variable of the template
parameter type as follows:

template <class T>
struct TemplateClass
{
T t;
};

struct TestClass
{
int x;
static TemplateClass<TestClass> v2;
};

Under Visual C++ 7 I get the following compiler error:
error C2079: 'TemplateClass<T>::t' uses undefined struct 'TestClass' with
[ T=TestClass ]

However, it compiles fine under the Borland compiler.

Is this a bug in the Visual C++ compiler?

I would appreciate any comments.

G++ (3.4.3) takes it as well. The circular references between the
template instantiation and its surrounding class is causing some
confusion.


It's no confusion. The code is illegal. According to §9 para 2, "a class
is considered defined after the closing brace of its class-specifier."
Therefore at the point of declaration of v2, TestClass has not been
defined yet, so the template TemplateClass<TestClass> cannot be
instantiated because it requires T to be a complete type.

Alberto

The definition you mentioned is a little fuzzy because static
variables are not part of the definition of objects of a class. They
are global variables scoped to objects of that class.

The following code does compile under VC++, which indicates to me that
the VC++ compiler is getting confused by the template.

struct TestClass
{
int x;
static TestClass v2;
};

Eric

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 22 '05 #11

"Alberto Barbati" <Al************@libero.it> schrieb im Newsbeitrag
news:nD********************@twister2.libero.it...
Antoun Kanawati wrote:
Eric wrote:
I have a template class that instantiates a variable of the template
parameter type as follows:

template <class T>
struct TemplateClass
{
T t;
};

struct TestClass
{
int x;
static TemplateClass<TestClass> v2;
};

Under Visual C++ 7 I get the following compiler error:
error C2079: 'TemplateClass<T>::t' uses undefined struct 'TestClass' with[ T=TestClass ]

However, it compiles fine under the Borland compiler.

Is this a bug in the Visual C++ compiler?

I would appreciate any comments.

G++ (3.4.3) takes it as well. The circular references between the
template instantiation and its surrounding class is causing some
confusion.


It's no confusion. The code is illegal. According to §9 para 2, "a class
is considered defined after the closing brace of its class-specifier."
Therefore at the point of declaration of v2, TestClass has not been
defined yet, so the template TemplateClass<TestClass> cannot be
instantiated because it requires T to be a complete type.

It is true that TestClass is not defined at the line in question.
But does it matter?

Isn't the line in question only a declaration?
9.4.2/2 explicitly says:
"The declaration of a static data member in its class definition is not a
definition .....The definition for a static data member shall appear in a
namespace scope enclosing the member's class definition":
I'd consider the code legal, because at the time v2 will be defined (outside
the class body), TestClass has to be defined.
Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #12
Thomas Mang wrote:
"Alberto Barbati" <Al************@libero.it> schrieb im Newsbeitrag
news:nD********************@twister2.libero.it...
>
> It's no confusion. The code is illegal. According to §9 para 2, "a class
> is considered defined after the closing brace of its class-specifier."
> Therefore at the point of declaration of v2, TestClass has not been
> defined yet, so the template TemplateClass<TestClass> cannot be
> instantiated because it requires T to be a complete type.

It is true that TestClass is not defined at the line in question.
But does it matter?

Isn't the line in question only a declaration?
9.4.2/2 explicitly says:
"The declaration of a static data member in its class definition is not a
definition .....The definition for a static data member shall appear in a
namespace scope enclosing the member's class definition":
I'd consider the code legal, because at the time v2 will be defined (outside
the class body), TestClass has to be defined.


It seem that you're right and I have been mistaken. As §9.4.2/2 says
that the type doesn't need to be complete, this use of the template does
not trigger implicit instantiation of the class template, according to
§14.7.1/4. VC++ is clearly trying to instantiate the template before
it's necessary, so it's definitely a compiler bug. As a double check I
tried with Comeau Online and in fact it reports the code to be legal.

Alberto

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #13
Thomas Mang wrote:

I'd consider the code legal, because at the time v2 will be defined (outside
the class body), TestClass has to be defined.


Want to laugh? I you modify the code in this way, even VC++ happily
accepts it, confirming that the code is legal and VC++ is buggy:

template <class T>
struct TemplateClass;

struct TestClass
{
int x;
static TemplateClass<TestClass> v2;
};

template <class T>
struct TemplateClass
{
T t;
};

Funny, isn't it?

Alberto

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #14

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

Similar topics

11
by: Rob Williscroft | last post by:
I have the following code , part of a trait to detect wether a type has a member operator "Type" (), it works fine with g++ 3.4 (msvc compiles it but it doesn't work for template'd operator T ())...
9
by: Vinodh Kumar P | last post by:
int main() { int aNormalInt = 0; short aShortInt = 0; aShortInt = aNormalInt; // Why I am not getting a warining for possible data lose in MS VC++6.0, even with warning level 4? return 0; } ...
4
by: veereshai | last post by:
i want to copy the functions from my source file into a new file...and convert each function into a new object file by compiling it. now, i want to invoke the function using the object file i have...
2
by: Bret Pehrson | last post by:
I just need to confirm now (after I've spent several days chasing mystery metadata warnings/errors) that this is *NOT* possible: Link a managed C++ static library into a (managed) C# application....
3
by: Chris Calzaretta | last post by:
From: "Chris Calzaretta" <ccalzaretta@hotmail.com> Subject: Re: Is It Possible? Date: Friday, February 04, 2005 11:44 AM Ok i am posting the code I need to create a form from this web service...
24
by: Arne Demmers | last post by:
Hi, As a project of mine I have to do some C programming, which in fact isn't the real problem. The problem it self is that I will have to write some code that is as minimal as possible, and as...
2
by: venkatbo | last post by:
Hi all, I have python2.4 running on ppc-linux 2.6.17. I'm attempting to get a TurboGears 0.9a9 (using CherryPy 2.2.1) based app running on it. During the TG-app startup sequence, it reaches...
1
by: Gerwin | last post by:
Hi, I have been using the interl IPP lib for some time on a microsoft compiler. Now i was wondering if I can switch to a gnu based compiler and still use the IPP. IPP uses .lib files where as...
71
by: Jack | last post by:
I understand that the standard Python distribution is considered the C-Python. Howerver, the current C-Python is really a combination of C and Python implementation. There are about 2000 Python...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.