我正在开发一个项目,似乎clang无法生成有效的字节码(因为链接器无法链接,链接时找不到模板类中的某些static constexpr
)
我可以使用类中的静态getter来修复它,但这会导致一些非常丑陋/重载的代码。
这是一个“最小”的代码示例,它会使 bug (它是一个bug?)出现。 不幸的是,在这里,g ++会产生相同的链接错误。
我在问这是不是编译器错误,或者我只是做错了什么,如果有解决方案可以避免这个错误。 (如果我做错了什么,为什么 相同的构造在我的大项目中起作用?)
注意:较大的项目在github上命名为yaggler
,但仍处于其生命的早期
#include <type_traits>
#include <iostream>
// exemple vector classes
struct vector2
{
constexpr vector2(unsigned int _x = 0, unsigned int _y = 0) : x(_x), y(_y) {} // for clang
unsigned int x;
unsigned int y;
};
struct vector3 : public vector2 // uh ;)
{
constexpr vector3(unsigned int _x = 0, unsigned int _y = 0, unsigned int _z = 0) : vector2(_x, _y), z(_z) {} // for clang
unsigned int z;
};
// simple templated generic vector type
// we could make a more generic one, but this would require something like a tuple.
template<unsigned int... Vals>
struct vector
{
static_assert(!(sizeof...(Vals) + 1), "[...]");
};
template<unsigned int X>
struct vector<X>
{
using vec_type = unsigned int;
static constexpr unsigned int value = X;
};
template<unsigned int X, unsigned int Y>
struct vector<X, Y>
{
using vec_type = vector2;
static constexpr vector2 value = vector2(X, Y);
};
template<unsigned int X, unsigned int Y, unsigned int Z>
struct vector<X, Y, Z>
{
using vec_type = vector3;
static constexpr vector3 value = vector3(X, Y, Z);
};
// a simple wrapper
template<typename V>
struct some_wrapper
{
static constexpr typename V::vec_type value = V::value;
};
// a dummy function that print something to stdout.
void do_something(int32_t id, const vector3 &value)
{
std::cout << id << " " << value.z << std::endl;
}
void do_something(int32_t id, const vector2 &value)
{
std::cout << id << " " << value.y << std::endl;
}
void do_something(int32_t id, int value)
{
std::cout << id << " " << value << std::endl;
}
// the class used to create the error
template< typename... Args>
class exemple
{
private:
// an initialisation that recurse over the Args... template arguments
template<typename Current, typename... Others>
void __rec_init() const
{
do_something(0, Current::value);
__rec_init<Others...>();
}
// end of recursion
template<size_t = 0>
void __rec_init() const {}
// launch the recursion
void tpl_init() const
{
__rec_init<Args...>();
}
public:
exemple()
{
tpl_init();
}
};
int main()
{
// and here, we get a linker error.
exemple<some_wrapper<vector<4, 4, 5>>, some_wrapper<vector<4, 1>>, some_wrapper<vector<9>>>();
}
编辑:仅提及gcc和clang版本:gcc 4.7.3 / 4.8.2和clang 3.2 / 3.3
答案 0 :(得分:2)
2个和3个模板参数的vector
类模板的特化分别得到了static constexpr
数据成员value
的文字类型vector2
和vector3
)没有命名空间范围定义。
你需要它们,因为当传递给value
函数时,它会在引用参数绑定时使用do_something
。
§9.4.2/ 3 [class.static.mfct]
如果非易失性
const static
数据成员是整数或枚举类型,则在类中声明它 定义可以指定brace-or-equal-initializer
,其中initializer-clause
的每个assignment- expression
都是一个常量表达式(5.19)。可以在。中声明文字类型的static
数据成员 使用constexpr
指定者进行课程定义;若然,其声明须指明brace-or-equal-initializer
其中initializer-clause
的每个assignment-expression
都是一个常量表达式。 [注意:两者都有 在这些情况下,成员可能出现在常量表达式中。 -end note] 该成员仍然应该被定义 在命名空间范围内,如果程序中使用了odr-used(3.2),则命名空间范围定义不得 包含初始化程序。
编辑:纠正自己,实际上some_wrapper<T>::value
需要这个定义(尽管如此,上面提到的原因)。所以你需要的是在some_wrapper
:
template<typename V>
constexpr typename V::vec_type some_wrapper<V>::value;
之后,您的代码compiles and runs。