LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++: How to put a pointer to a function, in a class template, without typedef'ing it? (https://www.linuxquestions.org/questions/programming-9/c-how-to-put-a-pointer-to-a-function-in-a-class-template-without-typedefing-it-866563/)

Aquarius_Girl 03-05-2011 04:44 AM

C++: How to put a pointer to a function, in a class template, without typedef'ing it?
 
Code:

#include <iostream>

template <class MyDummyClass, typename  (MyDummyClass ::*Method)(Parameter), typename Parameter>
class happy
{
    public:
          happy () {}
};

int main () {}

Code:

anisha@linux-uitj:~> g++ help.cpp -Wall
help.cpp:3: error: expected nested-name-specifier before ‘(’ token
help.cpp:3: error: expected ‘>’ before ‘(’ token


johnsfine 03-05-2011 06:18 AM

I think your two related threads would have been easier to answer if they were one thread. So I will add my answer for your question in this thread into my answer in the other thread:
http://www.linuxquestions.org/questi...6/#post4279709

bigearsbilly 03-05-2011 07:13 AM

don't avoid typedefs they are very good style.
look at the X windows headers as a good example.

ta0kira 03-05-2011 07:52 PM

You're using a value-based template parameter, not a type-based parameter; therefore, it won't compile with typename. Also, Parameter is undefined; if you move it to the second position and specify a return type for Method, it compiles. The problem is, you can't infer Parameter.
Code:

template <class MyDummyClass, typename Parameter, void (MyDummyClass::*Method)(Parameter)>
class happy
{
    public:
          happy () {}
};


struct unhappy
{
        void function(int) {}
};


static happy <unhappy, int, &unhappy::function> data;

Kevin Barry


All times are GMT -5. The time now is 06:09 AM.