constexpr 错误,std::is_same 具有多重继承
constexpr bug, std::is_same with multiple-inheritance
使用 VS2015 编译:
template <typename Owner, typename C, typename R, typename... Args>
constexpr bool FunctionBelongsTo(R(C::*)(Args...) const)
{
return std::is_same<C, Owner>::value;
}
class C
{
public:
int x;
};
class D
{
public:
int y;
};
class M : public C, public D
{
public:
void f() const {}
};
static_assert(FunctionBelongsTo<M>(&M::f) != true, "TRUE");
constexpr
函数给出了一个奇怪的错误:
error C2131: expression did not evaluate to a constant
note: a non-constant (sub-)expression was encountered
这个 std::is_same
表达式肯定是不变的。
奇怪的是,在 M
被多重继承的这种非常特殊的情况下,似乎会调用此错误。从 M
中删除基 类 中的任何一个,它编译得很好(即,它按预期发出 static_assert
),但是当 M
像这样被多重继承时,它决定表达式不是常量。
...是什么原因?菜鸟错误?
VS2015 Udpate 2 RC(CL 版本 19.00.23824.1)似乎解决了这个问题。您还可以在 http://webcompiler.cloudapp.net/ 上使用稍旧的版本进行验证,所以我希望它在更新进入 RTM 时保持不变。
使用 VS2015 编译:
template <typename Owner, typename C, typename R, typename... Args>
constexpr bool FunctionBelongsTo(R(C::*)(Args...) const)
{
return std::is_same<C, Owner>::value;
}
class C
{
public:
int x;
};
class D
{
public:
int y;
};
class M : public C, public D
{
public:
void f() const {}
};
static_assert(FunctionBelongsTo<M>(&M::f) != true, "TRUE");
constexpr
函数给出了一个奇怪的错误:
error C2131: expression did not evaluate to a constant
note: a non-constant (sub-)expression was encountered
这个 std::is_same
表达式肯定是不变的。
奇怪的是,在 M
被多重继承的这种非常特殊的情况下,似乎会调用此错误。从 M
中删除基 类 中的任何一个,它编译得很好(即,它按预期发出 static_assert
),但是当 M
像这样被多重继承时,它决定表达式不是常量。
...是什么原因?菜鸟错误?
VS2015 Udpate 2 RC(CL 版本 19.00.23824.1)似乎解决了这个问题。您还可以在 http://webcompiler.cloudapp.net/ 上使用稍旧的版本进行验证,所以我希望它在更新进入 RTM 时保持不变。