Thread: another template question

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

    another template question

    Is there any way to be able to do this:

    Code:
    class record : public base_record<record::pair_type> {
    public:
    	typedef std::pair<std::string, int> pair_type;
    	record() { }
    };
    I know pair_type typedef should be defined before using it.. Just wondering if this is possible?

    Thanks a lot for help

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use typename?
    Code:
    class record : public base_record<typename record::pair_type> {
    I think that would work.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    It doesnt.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I fiddled around with Dinkumware a little, but couldn't find a way to do this. Any variations, like this,
    Code:
    #include <iostream>
    #include <string>
    
    template <typename t>
    class base_record {};
    
    class record;
    typedef std::pair<std::string, int> record::pair_type;
    
    class record : public base_record<typename record::pair_type> {
    public:
        //typedef std::pair<std::string, int> pair_type;
        record() { }
    };
    
    int main() {
        return 0;
    }
    cause this sort of error:
    Code:
    sourceFile.cpp(8) : error C2027: use of undefined type 'record'
            sourceFile.cpp(7) : see declaration of 'record'
    I would suspect that you can't do this; however, I'm not sure. Maybe someone more knowledgeable about C++ will be able to tell you.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Impossible, sorry. But if you want to avoid redundant typing, perhaps you can put the pair_type typedef in the base class and pass the full type as a template parameter?
    Code:
    template<class Pair>
    class record_base
    {
    public:
      typedef Pair pair_type;
    };
    
    class record : public record_base< std::pair<...> >
    {
    };
    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. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 11:43 PM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. Template problem question
    By grscot in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2003, 12:31 PM