c – 隐式转换产生“错误:获取临时地址”(GCC vs clang)
发布时间:2020-12-16 07:23:51 所属栏目:百科 来源:网络整理
导读:在尝试使用强类型整数时,我遇到了来自GCC 8.2的奇怪错误: error: taking address of temporary 我可以想象上面的错误有意义的典型场景,但在我的情况下,我没有遇到问题.重现错误的缩小(设计)示例如下: #include cstddef#include type_traitsenum class Enum
在尝试使用强类型整数时,我遇到了来自GCC 8.2的奇怪错误:
我可以想象上面的错误有意义的典型场景,但在我的情况下,我没有遇到问题.重现错误的缩小(设计)示例如下: #include <cstddef> #include <type_traits> enum class Enum : std::size_t {}; struct Pod { std::size_t val; constexpr operator Enum() const { return static_cast<Enum>(val); } }; template<std::size_t N> constexpr void foo() { using Foo = std::integral_constant<Enum,Pod{N}>; // [GCC] error: taking address of temporary [-fpermissive] } int main() { foo<2>(); } 为什么GCC 8.2在这里抱怨? Clang 6.0很高兴(see goldbolt.org). 请注意,GCC存在第二个错误,可能有助于分析问题.我也不明白:
GCC 8.2的完整输出读取 <source>: In instantiation of 'constexpr void foo() [with long unsigned int N = 2]': <source>:22:10: required from here <source>:17:50: error: taking address of temporary [-fpermissive] using Foo = std::integral_constant<Enum,Pod{N}>; ^ <source>:17:50: error: no matching function for call to 'Pod::operator Enum(Pod*)' <source>:10:13: note: candidate: 'constexpr Pod::operator Enum() const' constexpr operator Enum() const { ^~~~~~~~ <source>:10:13: note: candidate expects 0 arguments,1 provided Compiler returned: 1 解决方法
这显然是一个错误; ‘Pod :: operator Enum(Pod *)’是胡说八道.您不能将参数传递给运算符Enum.
编译器似乎认为在编译器时将Foo转换为Enum的正确操作是foo.operator Enum(& foo)或其他东西.这两者都解释了“临时地址”和下一行. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |