I was experimenting with finding a type & then assigning the method, such as:
decltype(std::addressof(&std::vector<int>::push_back<int&&>)) x =
&std::vector<int>::push_back;
I was stuck at following error:
error: expected primary-expression before ‘
decltype
’
Above code is only as a minimal example. In reality, the address of the method will be passed to a template class along with its type. i.e. it can be &std::set<T>::insert<T&&>
as well. Hence auto
may NOT be an option.
See the pseudo code:
template<typename Type, // <-- int, float
template<typename...> class Container, // <-- std::vector, std::set
typename Method_t, // <-- decltype(push_back(T&&), insert(T&&))
Method_t Method> // <-- push_back(T&&), insert(T&&)
struct Wrap { ... };
#define WRAP(TYPE, CONTAINER, METHOD) \
Wrap<TYPE, CONTAINER, decltype(&CONTAINER<TYPE>::METHOD<TYPE&&>), &CONTAINER<TYPE>::METHOD>
Usage:
WRAP(int, std::vector, push_back); // uses `push_back(int&&)`
WRAP(float, std::set, insert); // uses `insert(float&&)`
What is the correct way to deduce the address of a template class's overloaded member method?
In my case, Method_t
is supposed to be any among the push_back
, insert
, push_front
, push
with the T&&
overload versions only.
This Qn didn't help: Address of templated member function