473,398 Members | 2,525 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,398 software developers and data experts.

Template code works with g++ but not MSVC++

The following code:

class ABC
{
std::list<ABC> mylist;
};

compiles in g++ without a problem. However, VC++ version 6 (latest service
pack), complains about the class ABC not being defined on the line where the
list is declared.

I tried adding a

class ABC;

statement before the class definition, but it had no effect. I understand
this problem is due to the fact that the template parameter of mylist is in
fact the class of which mylist is a member.

Is this a problem in microsoft's implementation, or is this normal behaviour
and the fact g++ accepts it is an extension? Are there any simple
workarounds? I need to get this code to work with both compilers.

Thanks
Pierre
Jul 22 '05 #1
6 1056
* Pierre-Marc Fournier:
The following code:

class ABC
{
std::list<ABC> mylist;
};

compiles in g++ without a problem. However, VC++ version 6 (latest service
pack), complains about the class ABC not being defined on the line where the
list is declared.

I tried adding a

class ABC;

statement before the class definition, but it had no effect. I understand
this problem is due to the fact that the template parameter of mylist is in
fact the class of which mylist is a member.

Is this a problem in microsoft's implementation
In VC6, yes. It only supports the most rudimentary features of templates.

I need to get this code to work with both compilers.


Indirection is the solution to every computer science problem.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...
* Pierre-Marc Fournier:
The following code:

class ABC
{
std::list<ABC> mylist;
};

compiles in g++ without a problem. However, VC++ version 6 (latest service pack), complains about the class ABC not being defined on the line where the list is declared.

I tried adding a

class ABC;

statement before the class definition, but it had no effect. I understand this problem is due to the fact that the template parameter of mylist is in fact the class of which mylist is a member.

Is this a problem in microsoft's implementation


In VC6, yes. It only supports the most rudimentary features of templates.


Bullshit. The C++ Standard requires that the element type for
any STL container be a complete type. You can get away with
an incomplete type if the library cuts corners on its
implementation of template class allocator, but don't count
on it.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #3
On Tue, 13 Jul 2004 11:46:57 -0400, "Pierre-Marc Fournier"
<pm*@users.sourceforge.net> wrote:
The following code:

class ABC
{
std::list<ABC> mylist;
};

compiles in g++ without a problem. However, VC++ version 6 (latest service
pack), complains about the class ABC not being defined on the line where the
list is declared.

I tried adding a

class ABC;

statement before the class definition, but it had no effect. I understand
this problem is due to the fact that the template parameter of mylist is in
fact the class of which mylist is a member.
Yes, inside the definition of ABC, ABC still has incomplete type.
Is this a problem in microsoft's implementation, or is this normal behaviour
and the fact g++ accepts it is an extension?
The latter; officially it is undefined behaviour. This particular
brand of undefined behaviour usually means it either works as you
expect, or you get a compiler error, depending on your
compiler/library combo.
Are there any simple
workarounds? I need to get this code to work with both compilers.


I note it seems to work with later versions of Dinkumware's library
(VC7+). The simplest workaround is to use
class ABC
{
std::list<ABC>* mylist;
};
since that doesn't instantiate std::list<ABC>, and doesn't require too
much housekeeping. std::list<ABC*> is an alternative, but that is much
more of a pain. shared_ptr<std::list<ABC> > is obviously the best bet.

Tom
Jul 22 '05 #4
* P.J. Plauger:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...
* Pierre-Marc Fournier:
The following code:

class ABC
{
std::list<ABC> mylist;
};

compiles in g++ without a problem. However, VC++ version 6 (latest service pack), complains about the class ABC not being defined on the line where the list is declared.

I tried adding a

class ABC;

statement before the class definition, but it had no effect. I understand this problem is due to the fact that the template parameter of mylist is in fact the class of which mylist is a member.

Is this a problem in microsoft's implementation
In VC6, yes. It only supports the most rudimentary features of templates.


Bullshit.


Sniff.

But language apart, which claim is it that in your opinion is "bullshit"?

Is it that you don't agree this particular problem is a problem in VC6?

That may be right, may be not, but it compiles with Comeau in strict mode,
so do you have, uh, chapter and verse here?

Is it that you don't agree VC6 only supports rudimentary features of
templates? If so, then that _is_ the word you used. Begging your pardon.
The C++ Standard requires that the element type for
any STL container be a complete type. You can get away with
an incomplete type if the library cuts corners on its
implementation of template class allocator, but don't count
on it.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #5
"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...
> Is this a problem in microsoft's implementation

In VC6, yes. It only supports the most rudimentary features of
templates.
Bullshit.
Sniff.

But language apart, which claim is it that in your opinion is "bullshit"?

Is it that you don't agree this particular problem is a problem in VC6?


Yes.
That may be right, may be not,
It is right. See below.
but it compiles with Comeau in strict mode,
Just because something compiles with a given compiler/library
combination doesn't mean it's conforming. In this case, much
depends on how accommodating template class allocator happens
to be. Some older ones are accommodating because they themselves
don't conform; some newer ones are because they go out of their
way to permit this extension.
so do you have, uh, chapter and verse here?
From microsoft.public.vc.language, courtesy of Igor Tadnetik:

: C++ standard 17.4.3.6/2:
:
: In particular, the effects are undefined in the following cases:
: ...
: - if an incomplete type (3.9) is used as a template argument when
: instantiating a template component.
:
: Your program invokes undefined behavior. It may work with some STL
: implementations but not others.
: --
: With best wishes,
: Igor Tandetnik
Is it that you don't agree VC6 only supports rudimentary features of
templates?
"Rudimentary" is, of course, a qualitative judgement. VC6 does not
support template partial specialization and export templates. In
its earliest days it got member templates wrong, but that got fixed
with service packs. There are one or two other problems as well.
Nevertheless, its template support is rich enough to support STL
well enough that people hit its limitations only occasionally.

Still, I wasn't complaining about your use of the word "rudimentary".
If so, then that _is_ the word you used. Begging your pardon.


Begging yours, but I aimed the word at your from-the-hip assertion
that this was a "problem with Microsoft's implementation" in VC6.
It isn't.

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #6
* P.J. Plauger:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...
> > Is this a problem in microsoft's implementation
>
> In VC6, yes. It only supports the most rudimentary features of templates.
Bullshit.
Sniff.

But language apart, which claim is it that in your opinion is "bullshit"?

Is it that you don't agree this particular problem is a problem in VC6?


Yes.
That may be right, may be not,


It is right. See below.


I agree, tentatively at least, but this is a subtle issue.

I do not think me being wrong about that qualified for the wording
you used.

It would have been different coming from e.g. me ( :-) ), but you
are the recipient of Dr Dobbs' award something-or-other, and generally
about as well-known as Bjarne, at least in the hard-core C++ community.

From microsoft.public.vc.language, courtesy of Igor Tadnetik:

: C++ standard 17.4.3.6/2:
:
: In particular, the effects are undefined in the following cases:
: ...
: - if an incomplete type (3.9) is used as a template argument when
: instantiating a template component.


§9.2/2 defines the detailed rules for where & when a class is regarded as
complete type versus incomplete type, it's not 100% simple.

And I'm not quite sure how the the standard allows constructs like
'class T: Base_<T>', when Base_<T> uses functions in T -- that it
does allow it is clear since this is very common, but not quite how; this
might seem irrelevant but was part of my belief support, so a
clarification here might be very helpful.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #7

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

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
5
by: Gianni Mariani | last post by:
The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1 dies complaining about somthing <unknown>. Is this valid ? More to the point, is there any way of doing this that is...
3
by: John Harrison | last post by:
This might be a compiler problem or it might be some subtlety of the language I don't understand. I'm writing an iterator and I want to write a version of std::distance specially for my...
1
by: icedac | last post by:
I have some questions about template and c++ itself. ///////////////////////////////////////////////////////////////////////// q1) see follow c++ code, and compile. it's works only IntelC++8.1...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
11
by: Niels Dekker - no reply address | last post by:
The following attempt to pass my template "Base" as a template template argument was rejected by Microsoft VC++ 8.0 (2005), while it still works on VC++ 7.1 (2003). Is it correct C++? And is...
2
by: Rafał Maj Raf256 | last post by:
Test case: #include <boost/shared_ptr.hpp> using namespace boost; template <typename typ1> class cData { public: template <typename T> T As() { } // As };
2
by: Glenn G. Chappell | last post by:
I am trying to write two constructors for the same class. One takes an iterator and so is a template. The other takes a particular type by reference to const. class Foo { public:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.