Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about out of line class definition

Reply
Thread Tools

question about out of line class definition

 
 
Ivan Sorokin
Guest
Posts: n/a
 
      10-23-2012
struct g
{
template <typename T>
struct z;
};

template <typename T>
struct x
{
typedef g y;
};

template <typename T>
struct x<T>::z
{
int b;
};

The code above is accepted by all major compilers. I wonder if this
code is correct. I haven't found any relevant information in the
standard.

Is this code correct? What is the general rule about how a name
qualifier in out of line class definition should be treated?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-23-2012
On 10/23/2012 7:39 AM, Ivan Sorokin wrote:
> struct g
> {
> template <typename T>
> struct z;
> };
>
> template <typename T>
> struct x
> {
> typedef g y;
> };
>
> template <typename T>
> struct x<T>::z
> {
> int b;
> };
>
> The code above is accepted by all major compilers. I wonder if this
> code is correct. I haven't found any relevant information in the
> standard.


The code above is OK syntactically. The problem with it (if any) will
only be exposed when you try to instantiate either x<> or g::z<>. Let's
see the code that actually *uses* those templates.

> Is this code correct? What is the general rule about how a name
> qualifier in out of line class definition should be treated?


Every template-id has to have the template arguments follow it unless
they are implicit. I don't see anything implicit here. x<T> and
g::z<T> are two unrelated templates AFAICT. So, my conclusion is that
the use 'z' in the definition of x::z requires the template argument
just like the use of 'x' would. Until you try to use 'x' or
'x<whatever>::z', you won't know where (or whether) you have made a
mistake.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Werner
Guest
Posts: n/a
 
      10-24-2012
On Tuesday, October 23, 2012 1:39:00 PM UTC+2, Ivan Sorokin wrote:
> struct g
>
> {
>
> template <typename T>
>
> struct z;
>
> };
>
>
>
> template <typename T>
>
> struct x
>
> {
>
> typedef g y;
>
> };
>
>
>
> template <typename T>
>
> struct x<T>::z
>
> {
>
> int b;
>
> };
>
>
>
> The code above is accepted by all major compilers. I wonder if this
>
> code is correct. I haven't found any relevant information in the
>
> standard.
>
>
>
> Is this code correct? What is the general rule about how a name
>
> qualifier in out of line class definition should be treated?


It is possible for z to be a non template when x has a
specialization e.g (an abc in this case here below):

struct g
{
template <class T> class z;
};

template <class T>
struct x
{
typedef g y; //z a template...
};

struct abc
{
typedef int type;
};

struct h
{
typedef abc z;
};

template <>
struct x<int>
{
typedef h y; //y an h for x<int>, which
// makes z an abc...
};

template <typename T>
struct x<T>::z
{
int b;
};

int main()
{
return x<int>::z::type( 0 );
}
 
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
Decralation of class inside other class and definition outside this class =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 2 07-12-2007 11:52 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Out-of-class definition of a non-template member of an explicitly specialized class template? BigMan C++ 3 04-22-2005 06:28 AM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 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