【发布时间】:2015-09-10 11:04:01
【问题描述】:
代码的第一个sn-p:
struct Base
{
int x{};
};
struct Derived :
Base
{
Derived()
: y{x}
{
}
int y;
};
int main()
{
Derived d;
}
编译正常:
第二个sn-p代码:
#include <type_traits>
template<int N>
struct Base
{
int x = N;
};
static const int When0 = -1;
template<int N>
struct Derived :
std::conditional<N == 0,
Base<When0>,
Base<N>>::type
{
Derived()
: y{x}
{
}
int y;
};
int main()
{
Derived<0> d;
}
编译正常:
不会编译:
要修复 gcc 和 clang,我需要指定 x 的类:
#include <type_traits>
template<int N>
struct Base
{
int x = N;
};
static const int When0 = -1;
template<int N>
struct Derived :
std::conditional<N == 0,
Base<When0>,
Base<N>>::type
{
using base_t = typename std::conditional<N == 0,
Base<When0>,
Base<N>>::type;
Derived()
: y{base_t::x}
{
}
int y;
};
int main()
{
Derived<0> d;
}
看(vc也会编译):
问题:哪个编译器是正确的?对此有何标准?
谢谢
【问题讨论】:
-
在 Visual Studio 中禁用“语言扩展”(
/Za) 会使其拒绝您的第二个 sn-p。经常使用的经验法则:当有疑问时,VC++ 是错误的。
标签: c++ visual-c++ gcc clang