template args question

This is a discussion on template args question within the C++ Programming forums, part of the General Programming Boards category; lets say i have some code Code: template <class Type1, class Type2 = ???> class Outer { class Inner {}; ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    template args question

    lets say i have some code
    Code:
    template <class Type1, class Type2 = ???>
    class Outer
    {
      class Inner
      {};
    };
    how could I get Inner to be the default argument for the template?
    Last edited by *ClownPimp*; 06-18-2003 at 07:09 PM.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Try giving it a forward declaration outside the class, perhaps.

    Code:
    class Outer::Inner;
    
    template <class Type1, class Type2 = Inner>
    class Outer
    {
      class Inner
      {};
    };
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Never mind... that failed horribly... I'll see if I can figure it out though.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    You might be able to get:

    class Outer;
    class Outer::Inner;

    class Outer { //...

    to work.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I tried many variations on that with GCC to no avail (nothing quite like guess-n-check programming, eh?).

    To be honest, I'm not entirely sure its possible. Since something inside the class is would be responsible in part for defining its own type, the logic seems like it'd become circular to have Inner as a valid template argument.

    Heres my rationale (anyone, feel free to shoot holes in this, I'm just trying to reason my way through this):

    You have four types, A1, A2, B1, B2.

    Outer<A1, A2> is a different type from Outer<B1, B2>, and consequently, Outer<A1, A2>::Inner is a different type from Outer<B1, B2>::Inner. Therefore, the type Outer::Inner is incomplete, and therefore, cannot fully define another type. It would be valid to have Outer<A1, A2>::Inner as one of the template parameters, but not Outer::Inner.

    Consider this:
    You have Outer<T1, Inner>. Inner is of course (as it is completely defined) Outer<T1, Inner>::Inner, so your declaration is something like this:

    Outer<T1, Outer<T1, Outer<T1, ... ad infinitum ...>::Inner>::Inner>, which clearly doesn't work.

    Anyways, I may be totally off, but it seems to make sense (at the moment at least).

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    That makes sense, thanks.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 09:44 AM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  3. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 11:43 PM
  4. Replies: 6
    Last Post: 12-06-2005, 09:23 AM
  5. templates with pointers
    By Cipher in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2002, 11:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21