簡體   English   中英

GCC 但不是 Clang 更改函數類型的 ref-qualifier 以獲取指向限定成員函數的指針

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM