Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我们有一个带有一个类的结构模板和一个指向该类成员的指针:
struct A<A,&A::a>
我不能这样声明模板
template<class T,class U> struct{};
我必须写
template<class T,typename T::type var> struct{};
为什么&A::a不能绑定成简单的typename T语法?在成为成员的指针之前,&A::a是一个类型,所以我们可以预期一个简单的类型名 T 可以工作,但事实并非如此
&A::a
typename T
&A::a是一个值,而不是一个类型。所以模板声明没有意义。
以下是它的工作方式:
template <typename A, int A::* Ptr> struct Foo { }; struct Bar { int n; }; int main() { Foo<Bar, &Bar::n> f; }
或者,更一般地说,
template <typename T, typename U, U T::* Ptr> struct Foo { }; Foo<Bar, int, &Bar::n> f;