mirt
mirt

Reputation: 1493

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?

Upvotes: 3

Views: 4157

Answers (1)

user405725
user405725

Reputation:

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;
}

Upvotes: 7

Related Questions