[英]Overloading a parent member function without ref-qualifier with a child member function with ref-qualifier in C++
[英]GCC but not Clang changes ref-qualifier of function type for a pointer to qualified member function
以下代碼片段在 Clang 中編譯,但在 GCC 12 中不編譯。
// function type (c style)
//typedef int fun_type() const&;
// C++ style
using fun_type = int() const&;
struct S {
fun_type fun;
};
int S::fun() const& {
return 0;
}
int main()
{
fun_type S::* f = &S::fun;
}
在 GCC 中產生錯誤:
prog.cc: In function 'int main()':
prog.cc:21:25: error: cannot convert 'int (S::*)() const &' to 'int (S::*)() const' in initialization
21 | fun_type S::* f = &S::fun;
| ^~~~~~~
S的聲明應該等同於以下聲明
struct S {
int fun() const&;
};
使用此聲明不會改變任一編譯器的行為。 這是編譯器翻譯模塊中與未充分使用的語言功能相關的錯誤嗎? 哪個編譯器在標准方面是正確的?
哪個編譯器在標准方面是正確的?
Clang 接受程序是正確的。 該程序格式良好,因為fun_type S::* f
等價於編寫:
int (S::*f)() const &
可以由初始化器&S::fun
初始化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.