静态表生成与GCC一起工作,但不是铛;是clang bugged? [英] Static table generation works with GCC but not clang; is clang bugged?

查看:178
本文介绍了静态表生成与GCC一起工作,但不是铛;是clang bugged?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编译时生成静态表/数组的一段时间写了一些代码,用于一些模板元编程(其想法是C语言字符串可以在编译时构建(它们只是 char arrays))。该想法和代码基于 David Lin answer

I wrote some code once upon a time that generated a static table/array at compile time for some template metaprogramming (the idea is that C-style strings can be built at compile time (they're just char arrays)). The idea and the code is based off David Lin's answer:

#include <iostream>

const int ARRAY_SIZE = 5;

template <int N, int I=N-1>
class Table : public Table<N, I-1>
{
public:
    static const int dummy;
};

template <int N>
class Table<N, 0>
{
public:
    static const int dummy;
    static int array[N];
};

template <int N, int I>
const int Table<N, I>::dummy = Table<N, 0>::array[I] = I*I + 0*Table<N, I-1>::dummy;

template <int N>
int Table<N, 0>::array[N];

template class Table<ARRAY_SIZE>;

int main(int, char**)
{
    const int *compilerFilledArray = Table<ARRAY_SIZE>::array;
    for (int i=0; i < ARRAY_SIZE; ++i)
        std::cout<<compilerFilledArray[i]<<std::endl;
}

使用GCC 4.9.2编译此代码工程:

Compiling this code with GCC 4.9.2 works:

$ g++-4.9 -Wall -pedantic b.cpp
$ ./a.out
0
1
4
9
16

Clang 3.5抱怨, / p>

Clang 3.5 complains, though:

$ clang++ -Wall -pedantic b.cpp
Undefined symbols for architecture x86_64:
  "Table<5, 0>::dummy", referenced from:
      ___cxx_global_var_init in b-b8a447.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

dummy 数组都在 Table 类(它们被声明的地方)之外。

dummy and array are both given definitions outside of the Table class (where they're declared). As far as I can tell, this should satisfy the linker requirements.

这是clang的错误吗?

Is this a bug with clang?

推荐答案

每个主要和部分专业化静态数据成员必须单独定义。

Every primary and partial specializations static data members must be defined separately.

template <int N, int I>
const int Table<N, I>::dummy = …;

这里定义的唯一的是 Table< N,I> :: dummy - 主要特殊化静态数据成员。 [temp.class.spec.mfunc] / 1 1

The only thing defined here is Table<N, I>::dummy - the primary specializations static data member. [temp.class.spec.mfunc]/11:


在某种程度上,需要定义需要定义的
; 主模板的
成员的定义从不用作类模板部分专门化的
成员的定义。

这也意味着GCC在这里是错误的。这是一个错误。

无论如何,添加

This also implies that GCC is wrong here. That's a bug.
Eitherway, adding

template <int N>
const int Table<N, 0>::dummy = 0;

应编译良好。



1)
特别是在上面引用的同一部分:


1) In particular, in the same section as the quote above:


类模板的一个成员的模板参数列表partial
specialization应该匹配
模板部分特化的模板参数列表。

$的模板参数列表b $ b类模板部分特化的成员应该匹配类模板部分特化的
模板参数列表。

The template parameter list of a member of a class template partial specialization shall match the template parameter list of the class template partial specialization.
The template argument list of a member of a class template partial specialization shall match the template argument list of the class template partial specialization.

这意味着用于定义部分专门化的参数列表及其成员必须相同。否则该成员从未定义。

That means that the argument lists used for defining the partial specialization and its member must be the same. Otherwise that member is never defined.

这篇关于静态表生成与GCC一起工作,但不是铛;是clang bugged?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆