Thread: Template arguement probelm.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149

    Template arguement probelm.

    This is probably something stupid but I can't figure out what's wrong with the following code:
    Code:
    template <typename STATE, typename UNAIRY_FUNCTION>
    std::vector<STATE> solvePuzzle(const STATE &start, const UNAIRY_FUNCTION goalfunc ){
        //compile error on next line. Says invalid template arguments.
        std::queue<std::set<STATE>::iterator> tryNext;
        ...
    }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I don't recall seeing predicate functions as typename anywhere in the STL.

    I'm also pretty sure that vectors and other containers need instantiable objects - use class.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by citizen View Post
    I don't recall seeing predicate functions as typename anywhere in the STL.

    I'm also pretty sure that vectors and other containers need instantiable objects - use class.
    I'm not sure what you mean but as far as I know the following are equal:
    template <typename STATE, typename UNAIRY_FUNCTION>
    template <class STATE, class UNAIRY_FUNCTION>
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by King Mir View Post
    I'm not sure what you mean but as far as I know the following are equal:
    template <typename STATE, typename UNAIRY_FUNCTION>
    template <class STATE, class UNAIRY_FUNCTION>
    As far as I know, they are equivalent in most modern compilers. However, the class keyword is the older style. If you're using a really old compiler, you might need to use class.

    On the other hand, I think that usage of the class keyword is deprecated and support for it might be removed in the future. So if your compiler supports it you should use typename.
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by King Mir View Post
    This is probably something stupid but I can't figure out what's wrong with the following code:
    Code:
    template <typename STATE, typename UNAIRY_FUNCTION>
    std::vector<STATE> solvePuzzle(const STATE &start, const UNAIRY_FUNCTION goalfunc ){
        //compile error on next line. Says invalid template arguments.
        std::queue<std::set<STATE>::iterator> tryNext;
        ...
    }
    You're missing a "typename":

    Code:
    std::queue<typename std::set<STATE>::iterator> tryNext;
    Whenever you use a scoping operator on a template-dependent typename, you need to say "typename" in front of it.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by citizen View Post
    I'm also pretty sure that vectors and other containers need instantiable objects - use class.
    There is absolutely no difference between the two in this context. There is only a difference when you use a template-template-parameter, as in:

    Code:
    template <template <typename> class C, typename X> // VALID
    template <template <typename> typename C, typename X> // INVALID

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by brewbuck View Post
    You're missing a "typename":

    Code:
    std::queue<typename std::set<STATE>::iterator> tryNext;
    Whenever you use a scoping operator on a template-dependent typename, you need to say "typename" in front of it.
    Thank you. Problem solved.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > On the other hand, I think that usage of the class keyword is deprecated and support for it might be removed in
    > the future.

    Are you sure? I've never heard this, and currently I use "class" if the corresponding argument is always a class, and "typename" otherwise.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, you're right. This usage of class is not actually deprecated. http://cboard.cprogramming.com/showp...81&postcount=5
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM