Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > issue on parashift faq section [39.14]

Reply
Thread Tools

issue on parashift faq section [39.14]

 
 
Diego Martins
Guest
Posts: n/a
 
      09-05-2007
http://www.parashift.com/c++-faq-lit...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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-05-2007
Diego Martins wrote:
> http://www.parashift.com/c++-faq-lit...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?


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-06-2007
Diego Martins wrote:

> http://www.parashift.com/c++-faq-lit...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?


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<>.

> 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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-06-2007
On Sep 5, 9:19 pm, Diego Martins <jose.di...@gmail.com> wrote:
> http://www.parashift.com/c++-faq-lit...sues.html#faq-...


> --------
> [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.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
section with in a section config file and reading that config file kampy Python 9 10-19-2012 10:59 PM
OT: parashift.com down, FAQ not being mirrored Keith Willis C++ 4 11-01-2007 10:46 PM
Parashift Down red floyd C++ 0 10-18-2007 10:07 PM
Your view of FAQ section. Richard C Programming 12 06-07-2007 11:35 AM
queries about the FAQ's section 7.5 jimjim C++ 10 09-14-2005 05:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57