为什么gcc警告关于decltype(main())而不是clang? [英] Why does gcc warn about decltype(main()) but not clang?

查看:248
本文介绍了为什么gcc警告关于decltype(main())而不是clang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码:

int main()
{
    decltype(main()) x = 0;
    return x;
}

gcc抱怨:

main.cpp: In function 'int main()':
main.cpp:8:19: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic]
     decltype(main()) x = 0;
                   ^
main.cpp:8:19: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic]

但不是clang。那么 decltype(main())会引发这个错误呢?如何 decltype 获取main的地址?

but not clang. So what about decltype(main()) raises this error? How does decltype take the address of main?

推荐答案

在这种情况下不能正确表达,因为 decltype 不需要知道 main 的地址;它只需要知道它的类型。但是,警告基于标准(§3.6.1/ 3)中的以下内容:

GCC's diagnostic might not be correctly phrased in this case, because decltype doesn't need to know the address of main; it only needs to know its type. However, the warning is based on the following from the standard (§3.6.1/3):


函数

The function main shall not be used within a program.

我假设GCC解释这意味着你可以甚至在一个未经评估的表达式中使用它。

I suppose GCC interprets this to mean that you can't even use it in an unevaluated expression.

Clang(3.4版本)似乎没有实现这个规则,即使我打开所有的标志认为,即使 main 以递归方式调用自身。这就是为什么它不给你警告。

Clang (version 3.4 anyway) appears to not implement this rule at all, even if I turn on all the flags I can think of and even if main calls itself recursively. That's why it doesn't give you a warning.

这篇关于为什么gcc警告关于decltype(main())而不是clang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆