question about out of line class definition

Discussion in 'C++' started by Ivan Sorokin, Oct 23, 2012.

  1. Ivan Sorokin

    Ivan Sorokin Guest

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

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

    template <typename T>
    struct x<T>::y::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?
    Ivan Sorokin, Oct 23, 2012
    #1
    1. Advertising

  2. 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>::y::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::y::z requires the template argument
    just like the use of 'x' would. Until you try to use 'x' or
    'x<whatever>::y::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
    Victor Bazarov, Oct 23, 2012
    #2
    1. Advertising

  3. Ivan Sorokin

    Werner Guest

    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>::y::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>::y::z
    {
    int b;
    };

    int main()
    {
    return x<int>::y::z::type( 0 );
    }
    Werner, Oct 24, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    565
    Victor Bazarov
    Mar 13, 2005
  2. BigMan
    Replies:
    3
    Views:
    1,416
    BigMan
    Apr 22, 2005
  3. Ark
    Replies:
    1
    Views:
    404
    Chris Torek
    Aug 7, 2004
  4. Jon Slaughter
    Replies:
    4
    Views:
    430
    Jon Slaughter
    Oct 26, 2005
  5. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Decralation of class inside other class and definition outside this class

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 13, 2007, in forum: C++
    Replies:
    2
    Views:
    332
    Alf P. Steinbach
    Jul 13, 2007
Loading...

Share This Page