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

M

Mikhail N. Kupchik

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
 
M

Maxim Yegorushkin

Mikhail N. Kupchik said:
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/
 
S

Sharad Kala

Mikhail N. Kupchik said:
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
 
T

Tom Widmer

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_;
};

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
 
J

Jonathan Turkanis

Mikhail N. Kupchik said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,010
Latest member
MerrillEic

Latest Threads

Top