模板的静态成员变量是定义的还是特化的?
Are static member variables of templates defined or specialized?
这涉及以下问题:constexpr differences between GCC and clang
在下面的 spine 中,最后一行是专业化、定义,还是两者兼而有之?
template<typename T>
struct A {
static const T s;
};
template<typename T>
const T A<T>::s = T(1);
这对我来说似乎是一个定义,但是由 gcc 成功编译的已发布问题让我质疑我的假设。
这是定义。
以下为专业。
template <>
const int A<int>::s = 20;
给定以下程序,
#include <iostream>
template<typename T>
struct A {
static const T s;
};
template <typename T>
const T A<T>::s = T(1);
template <>
const int A<int>::s = 20;
int main()
{
double a = A<double>::s;
double b = A<int>::s;
std::cout << "a: " << a << std::endl;
std::cout << "b: " << b << std::endl;
}
您应该期望输出为:
a: 1
b: 20
查看它的工作情况
这涉及以下问题:constexpr differences between GCC and clang
在下面的 spine 中,最后一行是专业化、定义,还是两者兼而有之?
template<typename T>
struct A {
static const T s;
};
template<typename T>
const T A<T>::s = T(1);
这对我来说似乎是一个定义,但是由 gcc 成功编译的已发布问题让我质疑我的假设。
这是定义。
以下为专业。
template <>
const int A<int>::s = 20;
给定以下程序,
#include <iostream>
template<typename T>
struct A {
static const T s;
};
template <typename T>
const T A<T>::s = T(1);
template <>
const int A<int>::s = 20;
int main()
{
double a = A<double>::s;
double b = A<int>::s;
std::cout << "a: " << a << std::endl;
std::cout << "b: " << b << std::endl;
}
您应该期望输出为:
a: 1
b: 20
查看它的工作情况