首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >取rvalue地址

取rvalue地址
EN

Stack Overflow用户
提问于 2022-02-16 01:22:47
回答 1查看 1K关注 0票数 16

为什么GCC版本的>= 10.1会用下面的代码产生taking address of rvalue错误?或者,当使用lvalue时,它为什么会抛出一个no matching function for call to 'a::n(a*)'错误?指针参数从何而来?Clang似乎完全可以接受代码(以及更老的GCC版本)。使用-std=c++14-std=c++17编译会产生相同的行为。

The 示例代码

代码语言:javascript
运行
复制
struct a {
    constexpr auto n() const { return 3; }
    //
    // static constexpr auto n() { return 3; }    // ok
    // auto n() const { return 3; }               // ok
};

template<typename>
constexpr auto f() {
    int{a{}.n()};      // error
    //
    int(a{}.n());      // ok
    int x{a{}.n()};    // ok
}

constexpr auto g() {
    int{a{}.n()};      // ok
    int(a{}.n());      // ok
    int x{a{}.n()};    // ok
}

产生的错误:

代码语言:javascript
运行
复制
<source>: In function 'constexpr auto f()':
<source>:10:14: error: taking address of rvalue [-fpermissive]
   10 |     int{a{}.n()};      // error
      |         ~~~~~^~
<source>:10:14: error: no matching function for call to 'a::n(a*)'
<source>:2:20: note: candidate: 'constexpr auto a::n() const'
    2 |     constexpr auto n() const { return 3; }
      |                    ^
<source>:2:20: note:   candidate expects 0 arguments, 1 provided
EN

回答 1

Stack Overflow用户

发布于 2022-03-05 17:57:49

正如注释和gcc bug跟踪器(gcc的布格波特)中所示,这是一个编译器错误,并且已经在gcc 12中修复。

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

https://stackoverflow.com/questions/71135361

复制
相关文章

相似问题

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