Template Non-Type参数,指向成员数据的指针

时间:2015-02-26 10:42:16

标签: c++ templates pointer-to-member non-type

在ISO_IEC_14882-2011中写入 数组元素的地址和非静态类成员的名称或地址是不可接受的模板参数。

[ Example:

template<int* p> class X { };
int a[10];
struct S { int m; static int s; } s;
X<&a[2]> x3; // error: address of array element
X<&s.m> x4; // error: address of non-static member
X<&s.s> x5; // error: &S::s must be used
X<&S::s> x6; // OK: address of static member

—end example ] 

所以我写了这个小例子并且遇到了不可理解的情况。

struct S
{
    int m;
    static int s;
} s;

template <int* p>
void foo()
{}

template <typename T, int T::* p>
void goo()
{}

int main()
{ 
   foo<&S::s>(); // **1. CASE** OK s is static member of struct S
   goo<S, &S::m>(); // **2. CASE** OK, but m is not static member of struct S
   goo<S, &S::s>(); // **3. CASE** error , while s is static member of S
                    // VisualStudio2013 error Failed to specialize function template 'void goo(void)'
}
                    //Clang error: no matching function for call to 'goo' goo<S, &S::s>(); // **3. CASE** error, while s is static member of S

那么当我参与成员所有者时情况发生了变化的原因是什么?如标准中所述,参数应该是来自类的静态数据成员的地址,但在2和3情况下反之亦然?有什么想法吗?

0 个答案:

没有答案