c++ - Use templates to bind a class method to a specific prototype -


i'm looking way bind functions , class methods specific prototype.

let's want bind functions , class methods prototype

int (float) 

to one

void () 

here code

class toto { public:     int test(float f) { std::cout << "toto::test " << f << std::endl; return 0; } } toto;  int test(float f) {     std::cout << "test " << f << std::endl;     return 0; }  template <typename t, t t> void func() {     t(4.0f); }  template <typename t> void func<int (t::*)(float), int (t::*method)(float)>() {     toto::*method(5.0f); }  auto    main(int, char**) -> int {     func<int(*)(float), &test>();     func<void (toto::*)(float), &toto::test>();  return exit_success; 

}

the function binding works properly, method 1 seems have syntax issues don't get. g++ gives me error :

src/main.cpp:28:6: error: parse error in template argument list src/main.cpp:28:55: error: function template partial specialization ‘func<int (t::*)(float), <expression error> >’ not allowed 

any ideas ?

you cannot partial specialize template function, can class/struct:

namespace details {     template <typename t, t t>     struct func_impl     {         void operator () () const { t(4.0f); }     };      template <typename t, int (t::*method)(float)>     struct func_impl<int (t::*)(float), method>     {         void operator () () const { (toto.*method)(5.0f); }     };  }     template <typename t, t t> void func() {     details::func_impl<t, t>{}(); } 

live demo


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -