470,815 Members | 1,201 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Template instantiation of std::list<> with incomplete type

Hi All.

I have a question regarding C++ programming language standard. It
is related to standard library, not to the core language.

Is it portable to instantiate template class std::list<> with
incomplete type? I've seen some STL implementations which allow this
and some others that does not. I did not find any mentioning of this
topic in the standard, maybe I searched not enough thoroughly?

It would be useful, for example, in such real code:

----------------------------------------------------------------

class TermArg;

class Term
{
...
std::list< TermArg > args_;
};

class TermArg
{
...
Term term_;
};

----------------------------------------------------------------

But is it portable to any standard-compliant STL implementation?
Same question for other STL template container classes.
-- Mikhail Kupchik

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

Jul 22 '05 #1
4 2316
Mikhail N. Kupchik <mi*****@mail.ru> wrote:
Is it portable to instantiate template class std::list<> with
incomplete type? I've seen some STL implementations which allow this
and some others that does not. I did not find any mentioning of this
topic in the standard, maybe I searched not enough thoroughly?


In short: it is not allowed to instantiate standard templates with
incomplete types.

You can find a long answer in article "The Standard Librarian: Containers
of Incomplete Types" by Matt Austern:
http://www.cuj.com/documents/s=7986/cujcexp2002austern/

--
Maxim Yegorushkin

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

"Mikhail N. Kupchik" <mi*****@mail.ru> wrote in message
Hi All.

I have a question regarding C++ programming language standard. It
is related to standard library, not to the core language.

Is it portable to instantiate template class std::list<> with
incomplete type? I've seen some STL implementations which allow this
and some others that does not. I did not find any mentioning of this
topic in the standard, maybe I searched not enough thoroughly?


Standard makes no such promise. The relevant section is 17.4.3.6/1 - "In
certain cases (replacement functions, handler functions, operations on types
used to instantiate standard library template components), the C++ Standard
Library depends on components supplied by a C++ program. If these components
do not meet their requirements, the Standard places no requirements on the
implementation. "

Later 17.4.3.6/2 says - "In particular, the effects are undefined in the
following cases: if an incomplete type is used as a template argument when
instantiating a template component."

Sharad

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3
On 14 Sep 2004 19:01:32 -0400, mi*****@mail.ru (Mikhail N. Kupchik)
wrote:
Hi All.

I have a question regarding C++ programming language standard. It
is related to standard library, not to the core language.

Is it portable to instantiate template class std::list<> with
incomplete type? I've seen some STL implementations which allow this
and some others that does not. I did not find any mentioning of this
topic in the standard, maybe I searched not enough thoroughly?
It isn't standard, but some implementations purposely provide it as an
extension (although I'm not sure whether they document the fact). The
relevent part of the standard is 17.4.3.6/2 (the last bit).
It would be useful, for example, in such real code:

----------------------------------------------------------------

class TermArg;

class Term
{
...
std::list< TermArg > args_;
};

class TermArg
{
...
Term term_;
};

----------------------------------------------------------------

But is it portable to any standard-compliant STL implementation?
I believe it's portable to libstdc++ and recent versions of
Dinkumware's library, at least.
Same question for other STL template container classes.


I think that std::map and std::set are the least likely to work, but
vector, deque and list often do work. If it doesn't work, you get a
diagnostic...

Tom

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

"Mikhail N. Kupchik" <mi*****@mail.ru> wrote in message
news:a0************************@posting.google.com ...
Hi All.

Is it portable to instantiate template class std::list<> with
incomplete type?


Maxim and Grzegorz are correct. I hope this restriction is relaxed somewhat,
since one of the virtues of shared_ptr is supposed to be that it can be
instantiated with incomplete types.

Jonathan

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Dave | last post: by
2 posts views Thread by Ben Pfaff | last post: by
17 posts views Thread by Isliguezze | last post: by
11 posts views Thread by paul.gibson | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.