我对使用clang 3.9编译的代码的行为感到有点困惑:
struct A {
constexpr A() = default;
A(const A&) = delete;
constexpr A(A&&) {}
A& operator =(const A&) = delete;
constexpr A& operator =(A&&) { return *this; }
constexpr operator bool() const { return &self == this; }
private:
A& self{*this};
};
constexpr A fooA() { return {}; }
int main(int argc, const char * argv[]) {
static_assert(fooA(), "");
return fooA();
}
Run Code Online (Sandbox Code Playgroud)
Godbolt链接:https://godbolt.org/g/CDFXAc
静态/编译时评估正确地发生fooA; 但是在运行时,构造函数似乎完全被省略.该static_assert不会触发(如预期),但主要还是返回0.是不是因为A是文字型还是因为编译器错误的?
在前者的情况下,任何对标准的引用都将受到赞赏.
nlohmann/json开源库的其中一个问题引起了我的注意.
我的情况下的最小再现那无法编译桌面GCC(4.8,4.9,还试图5+)的几个版本下却与Mac铛和Android NDK的GCC编译以及4.9
#include <limits>
struct base {
virtual void foo() = 0;
};
int main() {
(void)numeric_limits<base>::is_signed;
}
Run Code Online (Sandbox Code Playgroud)
GCC试图std::numeric_limits用基类实例化而不是派生:
/usr/include/c++/4.8/limits: In instantiation of 'struct std::numeric_limits<base>': main.cpp:11:94: required from here
/usr/include/c++/4.8/limits:309:7: error: cannot allocate an object of abstract type 'base'
min() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
Run Code Online (Sandbox Code Playgroud)
我不太确定这是一个已知的编译器错误(如果失败)或功能/放宽规则(如果成功)
我试图解决它,std::is_abstract但它没有帮助,看起来像'短路'评估没有发生在enable_if和错误保持不变
我的问题主要不是如何为gcc修复此问题,但天气这是编译器错误或代码错误
编辑:添加"更多最小"示例,没有任何标准库依赖:
template <typename T>
struct foo
{
static T bar();
static constexpr bool value = true;
};
struct abstract
{
virtual ~abstract() = …Run Code Online (Sandbox Code Playgroud)