【发布时间】:2016-04-03 14:28:02
【问题描述】:
考虑以下代码:
#include <memory>
void f( std::shared_ptr<int> ) {}
int main()
{
f( 0 ); // compiles fine in gcc and clang
f( 1 - 1 ); // compiles fine in gcc, fails in clang
constexpr int i = 0;
f( i ); // fails to compile in gcc and clang
f( i - 0 ); // compiles fine in gcc, fails in clang
}
为什么只有f( i ) 编译失败,而i 应该被评估为值为0 的编译时间常数?
PS 使用 g++ v 5.1.0 检查,在 c++11 和 c++14 模式下,它接受除 f(i); 之外的所有变体
PPS 使用 clang 3.7 检查,它在 c++11 和 c++14 模式下拒绝除文字 0 之外的所有变体
【问题讨论】:
-
编译器知道 0 是“有效”指针 (NULL),否则不是。我不认为这是正确的代码,即使其中一些有效。
-
我不确定
nullptr是否参与了这个问题;这是关于从int到void *的转换。 -
@Tommy 更改为
std::shared_ptr肯定是nullptr -
请注意,这可以更概括see the live example,我们可以看到clang像gcc一样接受C++03中更一般的例子。