gpt4 book ai didi

C++11 `nullptr_t` 返回函数被省略?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:35 25 4
gpt4 key购买 nike

以下 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();
}

这是正确的行为,还是编译器错误?

更新:

谢谢大家。仅供引用,这是我的解决方法:

 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 {}; }

最佳答案

这是 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以获得完整的修复。

关于C++11 `nullptr_t` 返回函数被省略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18666348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com