简体   繁体   中英

GCC but not Clang changes ref-qualifier of function type for a pointer to qualified member function

Following snippet compiles in Clang but not in 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;  
}

Produces error in 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;
      |                         ^~~~~~~

Declaration of S should be equivalent of following declaration

struct S {
    int  fun() const&;
};

Using this declaration doesn't change behaviour of either compiler. Is this a bug in compiler's translation module related to an under-used feature of language? Which compiler is correct standard-wise?

Which compiler is correct standard-wise?

Clang is correct in accepting the program. The program is well-formed as fun_type S::* f is equivalent to writing:

int (S::*f)() const &

which can be initialized by the initializer &S::fun .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM