编译模板类因G 或Clang 而失败
Compile template class failed by g++ or clang++
我编写了源代码,如下所示。-----样本。H-------
#include <iostream>
template <typename T>
class Sample {
private:
static int foo;
public:
Sample (T number) {
foo = number;}
void display () {
std :: cout << foo << std :: endl;}
};
---- test.cpp -------------------
#include "sample.h"
template <> int Sample <float> :: foo;
int main () {
Sample <float> test (100.9);
test.display ();
return 0;
}
我已经成功地使用Visual Studio 2015社区编辑。但是,G 和Clang (Ubuntu Linux 16.04 LTS)在链接时间时失败。我想编译G 或Clang ,所以我想做一些事情,我没有一个好主意。它不是与G 或Clang 规格兼容吗?那些熟悉编译器的人,不是吗?
gcc和clang是根据ISO C 标准的干燥字母正确的:
[temp.expl.spec]/13
模板或模板的静态数据成员的明确专业化 静态数据成员模板的明确专业化是一个 定义如果声明包括初始化器;否则,它 是声明。[注意:静态数据成员的定义 需要默认限制的模板必须使用 支撑列表:
template<> X Q<int>::x; // declaration template<> X Q<int>::x (); // error: declares a function template<> X Q<int>::x { }; // definition
- 终点注]
应用于示例时,意味着您只提供另一个声明,而不是定义。修复程序是添加初始化器:
template <> int Sample <float> :: foo{}; // Default initialize `foo`
或
template <> int Sample <float> :: foo{0.0}; // Direct initialize `foo` to 0
或
template <> int Sample <float> :: foo = 0.0; // Copy initialize `foo` to 0
相关文章:
- 结构化绑定初始值设定项表单 { 赋值表达式 } 对于 clang 上的数组类型失败
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 编译花絮 g++, clang++, 使用 libboost -- g++8 编译失败时 g++7 成功;
- 编译成功,使用 clang 5.0.1/6.0.0 ,在 5.0.2/6.0.1 时失败
- "Make"失败并出现 Clang 错误 - 如何从 Clang 获得错误?
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 带有引用的std::tuple在clang中编译失败,但在gcc中编译失败
- 构造函数SFINAE和继承在clang中失败
- 继承的构造函数,在 clang++3.9 中编译,在 g++ 7 中失败
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- C++指向成员的指针的类内初始化会使 MSVC 失败(但 GCC/Clang 工作)
- 在 GCC 中工作的外行构造函数模板在 Clang 中失败
- enable_if is_same constexpr函数使MSVC失败(但在Clang,GCC中效果很好)
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 线性重载:为什么 clang 在 GCC 编译时失败?
- 为什么 clang ++ 编译代码而 clang -x C++ 失败?ncurses with std::string.
- 为什么这个static_assert只在OSX上以clang失败,而在其他地方却没有