dimanche 3 avril 2022

template deduction of member functions

I'm trying to understand template argument deduction with regular functions, pointer to regular functions, member functions and pointer to member functions. Can someone explain why the last line yields a compile error while there is no issue with standalone?

#include <iostream>
#include <type_traits>
 
struct A {
    int fun(float, char) const&;
};
 
void standalone(int, char) {}

template <typename T>
void what(T &&) {
    std::cout << __PRETTY_FUNCTION__ << "\n";
}
 
int main() 
{
    what(standalone); // void what(T&&) [with T = void (&)(int, char)]
    what(decltype(&standalone){}); // void what(T&&) [with T = void (*)(int, char)]
    what(decltype(&A::fun){}); // void what(T&&) [with T = int (A::*)(float, char) const &]
    what(A::fun); // main.cpp: In function 'int main()':
                  // main.cpp:30:13: error: invalid use of non-static member function 'int A::fun(float, char) const &'
      |           // what(A::fun);
      |             ^~~
}

Aucun commentaire:

Enregistrer un commentaire