Код: Выделить всё
struct A
{
static const double i;
};
constexpr double A::i{};
constexpr double j = A::i; //valid c++ and accepted by all compilers.
И если я изменю double на int, то clang и gcc примут программу(
Код: Выделить всё
#2
Код: Выделить всё
template
class C
{
public:
static const double mem;
static const int k;
};
template
constexpr double C::mem{};
template
constexpr int C::k{};
int main()
{
constexpr double i = C::mem; //#1: gcc accepts but both clang and msvc rejcects
constexpr int j = C::k; //#2: gcc and clang accepts but msvc rejects
}
Для справки, вот ошибка clang для #1:
Код: Выделить всё
:14:22: error: constexpr variable 'i' must be initialized by a constant expression
14 | constexpr double i = C::mem; //#1: gcc accepts but both clang and msvc rejcects
| ^ ~~~~~~~~~~~~~~
:14:27: note: read of non-constexpr variable 'mem' is not allowed in a constant expression
14 | constexpr double i = C::mem; //#1: gcc accepts but both clang and msvc rejcects
| ^
:5:30: note: declared here
5 | static const double mem;
| ^
Подробнее здесь: https://stackoverflow.com/questions/782 ... -outside-t