Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer-to-member as template parameter deduction

I want to get pointer-to-member as template parameter to the foo1. Here is code:

struct baz{
    int qux;
};

template<typename C, typename T, T C::*m>
struct foo1{};

template<typename C, typename T>
void barr2(T C::*m){
}

template<typename C, typename T>
void barr1(T C::*m){
    barr2(m); // ok
    foo1<C, T, &baz::qux> _; // ok
    foo1<C, T, m> f; // g++4.6.1 error here; how to pass 'm' correctly ?
}

int main(){
    barr1(&baz::qux);
}

So how it should look like?

like image 603
mirt Avatar asked Dec 06 '25 20:12

mirt


1 Answers

It doesn't work for you because you are trying to use run-time information in a compile-time expression. It is the same as using integer that you read from console to specialize a template. It is not meant to work.

It doesn't necessarily solve your problem, but if the intent of barr1 function was to ease typing burden, something like this may work for you:

struct baz{
    int qux;
};

template<typename C, typename T, T C::*m>
struct foo1 {};

#define FOO(Class, Member)                                  \
    foo1<Class, decltype(Class::Member), &Class::Member>

int main(){
    FOO(baz, qux) f;
}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!