Thread: typename in class template

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    typename in class template

    If I have a templated class:

    Code:
    template <typename T>
    class someclass {
    public:
      typedef char sometype;
    };
    And I want to access typedef:

    Code:
    someclass::sometype (without making the object)
    compiler will give me error:
    error C2955: 'someclass' : use of class template requires template

    How to override this?

    Thanks for help!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You have to actually give the template name a parameter, e.g. someclass<int>::sometype, or something like that. In addition, if the type you give to it is a template parameter itself, you have to use the typename keyword.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by CornedBee View Post
    You have to actually give the template name a parameter, e.g. someclass<int>::sometype, or something like that. In addition, if the type you give to it is a template parameter itself, you have to use the typename keyword.
    Is there any other way beside passing the template parameter?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Make a typedef of the template class
    Code:
    typedef someclass<int> IntClass;
    
    IntClass::sometype;
    Or I think anyways

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But that's also passing the parameter. Which you have to do. Unless you're in the implementation of a member of the template, in which case you may omit the parameters and the compiler will just supply those of the instantiation you're in.

    See, thanks to template specialization, without knowing the actual parameter, the compiler doesn't know what type that is. It doesn't even know if such a member exists at all!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  2. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  3. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  4. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 09:50 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM