首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这可能是GCC的错误吗?

这可能是GCC的错误吗?
EN

Stack Overflow用户
提问于 2019-10-11 09:31:13
回答 1查看 177关注 0票数 3

我的团队内部实现了一些概念,我们遇到了GCC的问题。下面的代码将在Visual 2019上工作,但在GCC 8.3上失败:

代码语言:javascript
复制
#include <type_traits>
#include <iterator>

template <typename T>
constexpr auto name(int, T &instance = std::declval<T &>()) -> decltype(
    std::declval<decltype(std::begin(instance)) &>(), std::true_type{}) { return {}; }

template <typename>
constexpr auto name(...) -> decltype(std::true_type{}) { return {}; }

auto main() -> int {
    auto&& t =std::declval<nullptr_t>();
    name<nullptr_t>(0);
}

GCC 8.3在龙芯和我们的WSL安装返回以下错误:

代码语言:javascript
复制
In file included from <source>:1:

/opt/compiler-explorer/gcc-8.3.0/include/c++/8.3.0/type_traits: In instantiation of 'decltype (__declval<_Tp>(0)) std::declval() [with _Tp = std::nullptr_t; decltype (__declval<_Tp>(0)) = std::nullptr_t&&]':

<source>:12:39:   required from here

/opt/compiler-explorer/gcc-8.3.0/include/c++/8.3.0/type_traits:2058:21: error: static assertion failed: declval() must not be used!

       static_assert(__declval_protector<_Tp>::__stop,

                     ^~~~~~~~~~~~~~~~~~~~~~~~

Compiler returned: 1

能帮上什么忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-11 09:53:19

实际上,gcc是一个理智的编译器,考虑到您正在做的事情:

代码语言:javascript
复制
T &instance = std::declval<T &>()

在争论中。正如优先选择所说:

请注意,declval只能在未计算的上下文中使用,不需要定义;计算包含此函数的表达式是错误的。在形式上,如果这个功能是odr的话,程序是不正确的.

因此,这是比较差的检查从MSVC,而不是错误在GCC。正如您所看到的,declval是完全允许不定义的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58338312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档