使用 clang 和 G++ 编译此"simple"程序时出现链接错误
link error while compiling this "simple" program with clang and g++
我正在处理一个项目,似乎clang无法生成有效的字节码(由于链接器无法链接,在链接时找不到模板类中的某些static constexpr
) 我可以用类中的静态 getter 修复它,但这会导致一些非常丑陋/重载的代码。
这是一个"最小"的代码示例,它使错误(是错误吗?)出现。 不幸的是,在这里,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
vector
2 和 3 模板参数的类模板的专用化已获得static constexpr
没有命名空间作用域定义的文本类型(分别为vector2
和vector3
)的数据成员value
。
您将需要它们,因为当它传递给函数时绑定到引用参数时,您会value
odr-usedo_something
。
§9.4.2/3 [class.static.mfct]
如果非易失性
const static
数据成员是整型或枚举类型,则其在类中的声明 定义可以指定一个brace-or-equal-initializer
其中作为assignment- expression
的每个initializer-clause
都是常量表达式 (5.19)。文本类型的static
数据成员可以在 带有constexpr
说明符的类定义;如果是这样,其声明应具体说明brace-or-equal-initializer
其中作为assignment-expression
的每一initializer-clause
都是一个恒定的表达式。[注:在两者中 在这些情况下,成员可能出现在常量表达式中。—尾注 ]成员仍应定义 在命名空间作用域中,如果它在程序中是 ODR 使用的 (3.2) 并且命名空间作用域定义不应 包含初始值设定项。
编辑:纠正自己,实际上some_wrapper<T>::value
需要这个定义(尽管如此,出于上述原因)。所以你需要的是定义some_wrapper
之后的命名空间范围:
template<typename V>
constexpr typename V::vec_type some_wrapper<V>::value;
之后,您的代码将编译并运行。
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误