作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
看起来编译器接受不同的语法来初始化模板中的静态。
template <typename T> struct TBase
{
static const int i;
static const int j;
};
// compile: gcc + clang + visual + icc
template <> const int TBase<double>::i=1;
// compile: vc + icc
// failed gcc, gcc -fpermissive, clang
const int TBase<double>::j=2;
是没有template<>
的语法被标准接受,即使它目前显然不可移植?
编辑:在 vc++ 上使用此代码 TBase<double>::i==1
和 TBase<double>::j==2
完全像没有模板的代码。
struct noTemplate
{
static const int i;
static const int j;
};
const int noTemplate::i=1;
const int noTemplate::j=2;
gcc 和 clang 似乎强制使用 template<>
初始化这个静态,我不明白为什么编译器需要这个信息。
最佳答案
这两种语法都有效,但它们的含义不同。 template<>
的语法用于声明或定义隐式或显式实例化的成员:
template<class T> struct X { static int const i; };
template<> int const X<char>::i = 1; // Define member of an implicit instantiation
template struct X<long>;
template<> int const X<long>::i = 2; // Define member of an explicit instantiation
没有template<>
的语法用于定义特化的成员:
template<> struct X<float> { static int const j; }; // Class template specialization
int const X<float>::j = 3;
关于c++ - 模板特化静态初始化 icc+vc vs gcc+clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38884954/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
正如您在 this travis.yml 中看到的那样文件,我的代码依赖于一些第三方库,我在构建项目之前将它们安装在远程系统上。 Travis 每次推送提交时都会下载并构建这些库,这可以避免吗?我的意
我是一名优秀的程序员,十分优秀!