以下C++ 11程序在gcc 4.7.2下不输出任何内容:
#include <iostream>
using namespace std;
decltype(nullptr) g()
{
cout << "foo" << endl;
return nullptr;
}
int* f()
{
return g();
}
int main(int argc, char** argv)
{
auto x = f();
}
Run Code Online (Sandbox Code Playgroud)
这是正确的行为,还是编译器错误?
更新:
多谢你们.仅供参考我的解决方法:
struct NullPointer
{
template<class T> operator T*()
{
volatile decltype(nullptr) np = nullptr;
return np;
}
operator bool()
{
volatile bool b = false;
return b;
}
};
NullPointer g() { return {}; }
Run Code Online (Sandbox Code Playgroud)
这是 G++ 中的一个错误,它丢弃了类型表达式的副作用nullptr_t
它已针对 G++ 4.7.4 和 4.8.0 进行修复,请参阅http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52988了解初始不完整修复和http://gcc.gnu.org/bugzilla /show_bug.cgi?id=54170以获取完整修复。