c – std :: declval()触发断言错误,并在GCC中显示警告
发布时间:2020-12-16 09:46:08 所属栏目:百科 来源:网络整理
导读:请考虑以下代码段: #include utilitytemplate typename Uauto foo() - decltype(std::declvalU() + std::declvalU());template typename Tdecltype(fooT()) bar(T){}int main(){ bar(1); return 0;} 在使用-Wall -Wextra编译时,我在GCC的所有版本中都会发出
请考虑以下代码段:
#include <utility> template <typename U> auto foo() -> decltype(std::declval<U>() + std::declval<U>()); template <typename T> decltype(foo<T>()) bar(T) {} int main() { bar(1); return 0; } 在使用-Wall -Wextra编译时,我在GCC的所有版本中都会发出警告和静态断言失败(4.7.3,4.8.1,4.9-some-git).例如,这是4.8.1的输出: main.cpp: In instantiation of ‘decltype (foo<T>()) bar(T) [with T = int; decltype (foo<T>()) = int]’: main.cpp:12:7: required from here main.cpp:8:2: warning: no return statement in function returning non-void [-Wreturn-type] {} ^ In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.1/include/g++-v4/bits/move.h:57:0,from /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.1/include/g++-v4/bits/stl_pair.h:59,from /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.1/include/g++-v4/utility:70,from main.cpp:1: /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.1/include/g++-v4/type_traits: In instantiation of ‘typename std::add_rvalue_reference< <template-parameter-1-1> >::type std::declval() [with _Tp = int; typename std::add_rvalue_reference< <template-parameter-1-1> >::type = int&&]’: main.cpp:8:2: required from ‘decltype (foo<T>()) bar(T) [with T = int; decltype (foo<T>()) = int]’ main.cpp:12:7: required from here /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.1/include/g++-v4/type_traits:1871:7: error: static assertion failed: declval() must not be used! static_assert(__declval_protector::__stop, 如果要么使用return语句禁用警告或供应栏,例如, template <typename T> decltype(foo<T>()) bar(T a) { return a + a; } 断言失败消失了.在任何情况下,Clang 3.3都不会触发断言错误.这是GCC的符合标准的行为吗? 解决方法
只要我添加return {},我在6月初制作的GCC 4.9副本就会毫无怨言地编译它.或扔在功能里面.没有回报,它确实触发了静态断言.这肯定是一个错误,但只是一个小错误,因为该功能只在执行时“崩溃”.
我在尝试在常量表达式中执行declval()时看到了这个错误,所以在你的情况下可能会发生类似的事情. “不能使用”可能是指使用ODR或使用结果. 也许在没有任何声明的情况下,它试图制造具有decltype含量的声明.甚至添加一个简单的语句,如0;沉默的错误. (但static_assert(true,“”)或甚至void(0)都不足.) 提起了GCC bug. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |