Function name as template parameter?

This is a discussion on Function name as template parameter? within the C++ Programming forums, part of the General Programming Boards category; Is it possible to pass a function name as a template parameter? Here's an abstraction of what I'm trying to ...

  1. #1
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115

    Function name as template parameter?

    Is it possible to pass a function name as a template parameter? Here's an abstraction of what I'm trying to do. Another possible complication is that I'm trying to assign a default function that is part of a namespace:
    Code:
    namespace foo{
        void fighter();
    }
    
    template <function = foo::fighter>
    class a_class;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you mean, here? You don't ever specify anything about a typename when you declare a template.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Assuming that you want to define the class template instead of just declaring it, you might write this:
    Code:
    typedef void (*Function)();
    
    template<Function function = foo::fighter>
    class a_class;
    {
        // ...
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by laserlight View Post
    Assuming that you want to define the class template instead of just declaring it, you might write this:
    Yes, I do. That was just an abstraction.

    And the compiler accepted that, so I guess that was it. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. More explicit template member function hell
    By SevenThunders in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2009, 11:36 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  4. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 05:37 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM

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