指针到成员作为模板参数推导 [英] Pointer-to-member as template parameter deduction
本文介绍了指针到成员作为模板参数推导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将指向成员的指针作为foo1的模板参数。这里是代码:
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?
推荐答案
它不适用于你,因为你试图在编译时表达式中使用运行时信息。它与使用从控制台读取的专门化模板的整数相同。
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.
这不一定能解决你的问题,但如果 barr1
函数是为了减轻打字负担,这样的东西可能为你工作:
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;
}
这篇关于指针到成员作为模板参数推导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文