c++ - Deduce first template parameter in multiple parameter template by parameter -


first question , explanation of i'm trying might approaching problem wrong.

is possible deduce first template parameter in multi parameter template parameters while specifying other parameters.

example:

 template<class a, class b>  b function(a object)  {       return b(a);  } 

called like:

 function<btype>(ainstance); 

i not find way make work.

edit: example might fit better problem below indicated me first comment got

example:

 template<class a, a::b foo>  void function(a object)  {       object.dosomethingwithtypea::b(foo);  } 

called like:

 function<a::binstance>(ainstance); 

the difference second template parameter dependent on specific type of first can not switch template parameters around.

now description of trying do:

i trying create templates universal functor class can take either free function or member function , wrap functor.

i succeeded in set out want make bit more user friendly. templated function create functor looks like:

template <class t, returntype (t::*method)(functionarguments...)> static constexpr universalfunctor create(t* object) {    return {object, methodwrapper<t, method>}; } 

right create functor member function user has call

universalfunctor<returntype>::create<classname, classfunction>(object) 

which rather cumbersome.

since object has of type classname or @ least deducible have expected call like:

universalfunctor<returntype>::create<classfunction>(object) 

should able succeed classname parameter deducible pointer passed. not appear possible classfunction treated first template parameter.

no, can specify template arguments in order of declaration. so, can specify first , let second deduced, not other way around.

you write wrapper template:

template<class b, class a> b function_wrapped(a&& object) {      return function<a, b>(std::forward<a>(object)); } 

now can call:

function_wrapped<btype>(ainstance); 

of course, simpler change order of parameters in original function.

the second example quite bit more problematic.

if can reverse dependency of types. requiring b defines alias a, define:

template<class b, b foo> void function(typename b::a object) 

and call

function<decltype(binstance), binstance>(ainstance); 

it's not pretty.

the simplest solution use runtime non-type arguments types can deduced.

template<class a, class b> void function(a object, b foo) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -