303*_*303 17 c++ c++14 c++17 c++20
为什么 GCC version >=10.1会产生taking address of rvalue以下代码的错误?或者,当使用左值时,为什么它会抛出错误no matching function for call to 'a::n(a*)'?指针参数从哪里来?Clang 似乎完全可以接受代码(以及旧的 GCC 版本)。使用-std=c++14或-std=c++17进行编译会产生相同的行为。
示例代码:
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
}
Run Code Online (Sandbox Code Playgroud)
产生的错误:
<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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1679 次 |
| 最近记录: |