2015-08-25 113 views
1

我只想了解编译器为什么会给出这个错误以及需要更改哪些内容?我只是在玩C++ 11代码。为什么限定名称错误?

class test 
{ 
public: 
    template<int N> 
    bool foo() { return true;} 

    template<int N, int... Ns> 
    struct table : table<N-1, N-1,Ns...> {}; 

    template<int... Ns> 
    struct table<0,Ns...> 
    { 
     static constexpr bool(test::table<0,Ns...>::*fns[])() = {foo<Ns>...}; 
    }; 

    // below line gives the error 

    template<int... Ns> 
    constexpr bool (*test::table<0,Ns...>::fns[sizeof...(Ns)])(); 
}; 

int main() 
{ 
} 

的错误是:

error: invalid use of qualified-name test::table<0, Ns ...>::fns

+0

这个代码有很多问题。例如,'foo'是一个“'test :: *'”,而不是“'test :: table <0, Ns...> :: *'”。这些问题中的大部分都与您显示的错误无关。 – chris

+0

你想做什么?如果通过倒数第二行来声明'test'的成员函数,则必须给该成员函数一个非限定名称。 – JohnB

+0

@JohnB,我认为OP正试图定义静态成员'fns'。这个定义不应该在课堂上。 – chris

回答

0

test::table可能是test成员模板,但它并没有给test的成员函数不合格访问。这是我们假定你是什么样的位置:

= {foo<Ns>...}; 
// ^^^ 

您使用一个合格的-ID为指针到成员得到很好的形成(即&test::foo<Ns>)。此外,table专业化后的fns的“定义”不能进入类范围内。静态数据成员定义进入命名空间范围

您还需要在此定义中将*test::table更改为test::*table,因为后者正确表示指向成员的指针。 @chris's example shows the correct code


你也可以做掉与table模板,并使用内置std::index_sequence的工具,如果你有机会到C++ 14。

#include <utility> 

struct test { 
    template<int>bool foo(){return true;} 
    template<int...Is> 
    static auto& make_foo_table(std::index_sequence<Is...>){ 
     using callable=bool(test::*)(); 
     using table=callable[sizeof...(Is)]; 
     return table{&test::foo<Is>...}; 
    } 
    template<int N> 
    constexpr static auto&make_foo_table(){ 
     return make_foos(std::make_index_sequence<N>{}); 
    } 
}; 
相关问题