2011-08-20 55 views
0

我试图用锵3.0编译下面代码静态数组。 它没有链接,我在这里做得不对或这是一个编译器错误?奇怪接头误差的INT

错误遵循建筑x86_64的

未定义符号:在CC-JDTbNl.o LD __ZN9int_arrayIJLi0ELi1ELi2ELi3ELi4ELi5ELi6ELi7ELi8EEE5printEv ::未找到架构x86_64的

符号(多个)
“__ZN9int_arrayIJLi0ELi1ELi2ELi3ELi4ELi5ELi6ELi7ELi8EEE4listE”, 从参考

的代码如下

#include <iostream> 

static const int a[] = {0,1,2,3,4,5,6,7,8}; 

template<int... Numbers> struct int_array; 

template<int... Numbers> 
struct int_array { 
    int x; 
    const static int list[] = {Numbers...}; 
    static void print() { 
    for (const int x : list) { 
     std::cout << x <<std::endl; 
    } 
    } 
    static void print2() { 
    for (const int x : a) { 
     std::cout << x <<std::endl; 
    } 
    } 
}; 

typedef int_array<0,1,2,3,4,5,6,7,8> array_of_ints; 

int main (int argc, const char * argv[]) 
{ 
    array_of_ints::print(); 
    array_of_ints::print2(); 
    return 0; 
} 

回答

1

我不上的C++ 0x所有的专家,希望有人更了解会来这里......但的C++ 0x允许在课堂上静态成员的初始化?如果是这样,叮当还没有实现它。如果没有,你不能。 The (almost) standard,9.4.2项目3,说,任何常量文本类型可以initiazlied; 3.9条款10表示int数组是一个文字。所以,我想这是铛3.0的一个bug,但有可能会进一步规则的可变参数模板的情况下...

不管怎样,改变你的代码如下所示为我工作:

template<int... Numbers> 
struct int_array { 
    int x; 
    const static int list[]; 
    static void print() ; 
}; 

template<int... Numbers> 
const int int_array<Numbers...>::list[]={Numbers...}; 

template<int... Numbers> 
void int_array<Numbers...>::print(){ 
    for (const int x : list) { 
     std::cout << x <<std::endl; 
    } 
} 
+0

我想在类初始化仅允许用于静态常量* *不可或缺的成员。 –

+0

谢谢你对此的回应我会将这个作为可能的bug提交给叮当声的人 – James

+0

@Kerek我认为这是C++ 03的情况。在C++ 11中,可以对任何文字常量静态成员进行类内初始化。 – Yuji

2

你编码是错误的,原因有二

  • 如果不是整数或枚举类型,而不是constexpr
  • 你错过了一个定义int_array<yournumbers>::list您不能初始化类的静态数据成员。当你使用该成员时,你需要定义它。

这不是一个铿锵错误。一旦叮当获得constexpr支持,并且当你添加out类的定义(这样就不能有初始化器),并且你已经提供了一个类),并且用constexpr代替const,代码就可以正常工作。


对于大众,here is the PR that @James sent to Clang