Search code examples
c++pointer-to-member

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?


Solution

  • 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.