简体   繁体   English

编译模板类因g ++或clang ++失败

[英]Compile template class failed by g++ or clang++

I wrote the source code as shown below. 我编写了如下所示的源代码。 ----- sample.h ------ ----- sample.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 -------------- ---- test.cpp --------------

#include "sample.h"
template <> int Sample <float> :: foo;


int main () {
 Sample <float> test (100.9);
 test.display ();
 return 0;
}

I have successfully compiled with Visual Studio 2015 community. 我已成功使用Visual Studio 2015社区进行了编译。 However, g++ and clang++ (ubuntu linux 16.04 LTS) failed at linking time. 但是,g ++和clang ++(ubuntu linux 16.04 LTS)在链接时失败。 I want to compile with g++ or clang++, so I'd like to do something, I do not get a good idea. 我想用g ++或clang ++进行编译,所以我想做点什么,我没有一个好主意。 Is not it compatible with g++ or clang++ specifications? 它与g ++或clang ++规范不兼容吗? Are those who are familiar with the compiler, are not you? 是那些熟悉编译器的人,不是吗?

GCC and Clang are correct according to the dry letter of the ISO C++ standard: 根据ISO C ++标准的大写字母,GCC和Clang是正确的:

[temp.expl.spec]/13 [temp.expl.spec] / 13

An explicit specialization of a static data member of a template or an explicit specialization of a static data member template is a definition if the declaration includes an initializer; 如果声明包含初始化程序,则模板的静态数据成员的显式专业化或静态数据成员模板的显式专业化是定义。 otherwise, it is a declaration. 否则,它是一个声明。 [ Note: The definition of a static data member of a template that requires default-initialization must use a braced-init-list: [注意:需要默认初始化的模板的静态数据成员的定义必须使用大括号初始化列表:

 template<> X Q<int>::x; // declaration template<> X Q<int>::x (); // error: declares a function template<> X Q<int>::x { }; // definition 

— end note ] —尾注]

Which when applied to your example means you just provide another declaration, not a definition. 将其应用于您的示例意味着您仅提供了另一个声明,而不是定义。 And a fix would be to add an initializer: 解决方法是添加一个初始化程序:

template <> int Sample <float> :: foo{}; // Default initialize `foo`

Or 要么

template <> int Sample <float> :: foo{0.0}; // Direct initialize `foo` to 0

Or 要么

template <> int Sample <float> :: foo = 0.0; // Copy initialize `foo` to 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有已删除“一般”情况的专用模板功能无法使用g ++ &lt;= 4.8.0和clang ++进行编译 - Specialized template function with deleted “general” case fails to compile with g++ <=4.8.0 and clang++ 无法使用 std::enable_if 在 g++/clang++ 上编译显式专用模板结构 - Failing to compile explicitly specialized template struct on g++/clang++ with std::enable_if g ++和clang ++在模板类中定义的朋友模板函数的不同行为 - g++ and clang++ different behaviour with friend template function defined inside a template class 代码在c ++中无法在g ++中编译 - code does not compile in g++ while it does in clang++ g ++中的奇怪编译错误(clang ++可以正常编译) - Strange Compile Errors in g++ (clang++ compiles fine) 使用 std::acos 和 clang++ 而不是 g++ 的 Constexpr 编译错误 - Constexpr compile error using std::acos with clang++ not g++ 有没有办法让 G++/clang++ 的编译时间和 MSVC 一样快? - Is there a way to make G++/clang++ compile times as fast as MSVC? 在类中定义的友元函数模板是否可用于查找? clang ++和g ++不同意 - Is a friend function template defined in the class available for lookup? clang++ and g++ disagree 使用clang ++和g ++,双嵌套模板类中的c ++ static int def失败 - c++ static int def in doubly nested template class fails with clang++ and g++ 这是 g++ 或 clang++ 中的错误 - Is this a bug in g++ or clang++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM