为什么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 示例代码
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
}
产生的错误:
<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
发布于 2022-03-05 17:57:49
正如注释和gcc bug跟踪器(gcc的布格波特)中所示,这是一个编译器错误,并且已经在gcc 12中修复。
https://stackoverflow.com/questions/71135361
复制相似问题