Home Member function & const member function pointer deduction
Reply: 1

Member function & const member function pointer deduction

ixSci
1#
ixSci Published in 2012-04-18 07:57:11Z

I have the following code:

template <class Ret>
class Foo
{
public:
    template <class T>
    void foo(T&, const std::function<Ret()>&)
    {
        std::cout << "std::function<Ret()>\n";
    }
    template <class T>
    void foo(T&, Ret(T::*)() const)
    {
        std::cout << "Ret(T::*)() const\n";
    }
    template <class T>
    void foo(T&, Ret(T::*)())
    {
        std::cout << " Ret(T::*)()\n";
    }
};

class A
{
public:
    void foo1() const
    {
    }
    void foo()
    {
    }
};

 int main()
 {

     A a;
     Foo<void> f;
     f.foo(a, &A::foo);
     f.foo(a, &A::foo1);
     f.foo(a, std::bind(&A::foo, a));
 }

It works well, but I don't want to have 2 different function for const and non-const member function pointer. So the question is: is there a way to merge void foo(T&, const std::function&) & void foo(T&, Ret(T::*)() const) to one function? Note, that there is std::function overload which should also participate in resolution after merge. I need some function which will take member function pointers only. And all other will make its way to std::function version.

Thanks!

Johannes Schaub - litb
2#
Johannes Schaub - litb Reply to 2012-04-18 08:11:51Z

You seem to be asking one implicit question and one explicit question. The answer to your implicit question, "how to merge the const and nonconst versions", is as follows

template<typename T, typename U, typename enable_if<is_fuction<T>::value, int>::type = 0> 
void takesboth(T U::*);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.322946 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO