我的团队内部实现了一些概念,我们遇到了GCC的问题。下面的代码将在Visual 2019上工作,但在GCC 8.3上失败:
#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安装返回以下错误:
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能帮上什么忙吗?
发布于 2019-10-11 09:53:19
实际上,gcc是一个理智的编译器,考虑到您正在做的事情:
T &instance = std::declval<T &>()在争论中。正如优先选择所说:
请注意,declval只能在未计算的上下文中使用,不需要定义;计算包含此函数的表达式是错误的。在形式上,如果这个功能是odr的话,程序是不正确的.
因此,这是比较差的检查从MSVC,而不是错误在GCC。正如您所看到的,declval是完全允许不定义的。
https://stackoverflow.com/questions/58338312
复制相似问题