issue on parashift faq section [39.14]

D

Diego Martins

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.14

--------
[39.14] Why can't I put a forward-declared class in a std::vector<>?
....
Just remember this: Whenever you use a class as a template parameter,
the declaration of that class must be complete and not simply forward
declared.
--------

is this endorsed by the standard?
AFAIK, this is dependent of the class template specification.

For example, I was able to declare vectors with incomplete types in
more than one compiler.

Diego
 
V

Victor Bazarov

Diego said:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.14

--------
[39.14] Why can't I put a forward-declared class in a std::vector<>?
...
Just remember this: Whenever you use a class as a template parameter,
the declaration of that class must be complete and not simply forward
declared.

It's not explicitly specified one way or the other. However, there is
no requirement in the Standard that a template type argument be a complete
type.
AFAIK, this is dependent of the class template specification.
Yes.

For example, I was able to declare vectors with incomplete types in
more than one compiler.

I believe it all hinges on the meaning of the word "use". Unless you
are instantiating the template, the type argument can be incomplete.
When the instantiation takes place, the type is better be complete;
of course it also depends on the template implementation...

V
 
K

Kai-Uwe Bux

Diego said:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.14

--------
[39.14] Why can't I put a forward-declared class in a std::vector<>?
...
Just remember this: Whenever you use a class as a template parameter,
the declaration of that class must be complete and not simply forward
declared.

In full generality, no. It is possible to design templates that produce
well-defined behavior (even desired behavior) when used with incomplete
types (of course, the type has to be complete at some point). One example
of such a template is tr1::shared_ptr said:
AFAIK, this is dependent of the class template specification.
Right.

For example, I was able to declare vectors with incomplete types in
more than one compiler.

Now, that goes against the standard. If you use any of the standard
containers with an incomplete type, you have undefined behavior. See
[17.4.3.6, last item].


Best

Kai-Uwe Bux
 
J

James Kanze

--------
[39.14] Why can't I put a forward-declared class in a std::vector<>?
...
Just remember this: Whenever you use a class as a template parameter,
the declaration of that class must be complete and not simply forward
declared.
--------
is this endorsed by the standard?

I'd need more context to be sure. In general, the standard only
requires a complete definition where something is instantiated
in the template which requires a complete definition. And it is
very conservative with regards to what may be instantiated. The
question, however, involves std::vector, and the standard says
very explicitly that instantiating any part of any of the
standard templates over an incomplete type is undefined
behavior.
AFAIK, this is dependent of the class template specification.

It is dependent on the specification. The specification for the
templates in the standard says that the type must be complete,
and that it is undefined behavior otherwise.
For example, I was able to declare vectors with incomplete
types in more than one compiler.

It's undefined behavior. It might work. Sometimes. (It will
fail to compile with recent versions of g++, at least if the
usual options are given.)
 

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,999
Messages
2,570,244
Members
46,839
Latest member
MartinaBur

Latest Threads

Top