这是 GCC 中可能存在的错误吗?
Is this a possible bug in GCC?
我的团队在内部实施概念,我们遇到了 GCC 的一个错误。以下代码将在 Visual Studio 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);
}
在 Godbolt 和我们的 WSL 安装上的 GCC 8.3 returns 出现以下错误:
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
任何关于这可能是什么的帮助?
实际上,考虑到您正在做的事情,gcc 是这里最健全的编译器:
T &instance = std::declval<T &>()
在争论中。正如 cppreference 所说:
Note that declval can only be used in unevaluated contexts and is not
required to be defined; it is an error to evaluate an expression that
contains this function. Formally, the program is ill-formed if this
function is odr-used.
所以这是 MSVC 的错误检查而不是 GCC 中的错误。 declval
如你所见,完全可以不定义。
我的团队在内部实施概念,我们遇到了 GCC 的一个错误。以下代码将在 Visual Studio 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);
}
在 Godbolt 和我们的 WSL 安装上的 GCC 8.3 returns 出现以下错误:
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
任何关于这可能是什么的帮助?
实际上,考虑到您正在做的事情,gcc 是这里最健全的编译器:
T &instance = std::declval<T &>()
在争论中。正如 cppreference 所说:
Note that declval can only be used in unevaluated contexts and is not required to be defined; it is an error to evaluate an expression that contains this function. Formally, the program is ill-formed if this function is odr-used.
所以这是 MSVC 的错误检查而不是 GCC 中的错误。 declval
如你所见,完全可以不定义。