C++ 当与函数指针一起使用时,引擎盖下的auto关键字是什么?

C++ 当与函数指针一起使用时,引擎盖下的auto关键字是什么?,c++,constants,constexpr,auto,member-function-pointers,C++,Constants,Constexpr,Auto,Member Function Pointers,在试验自动和函数指针时,我注意到一个奇怪的行为 class Test { public: void Func(){} }; static constexpr const auto member_ptr1{ &Test::Func }; // compile fine static constexpr const void(Test::*member_ptr2)(void) { &Test::Func }; // ERROR : cannot convert from v

在试验
自动
和函数指针时,我注意到一个奇怪的行为

class Test
{
public:
    void Func(){}
};

static constexpr const auto member_ptr1{ &Test::Func }; // compile fine
static constexpr const void(Test::*member_ptr2)(void) { &Test::Func }; // ERROR : cannot convert from void(Test::*)(void) to const void(Test::* const)(void)
我知道,在
成员ptr2
的情况下,编译器抱怨没有找到签名为
const void Func()
的函数,但它应该会触发
成员ptr1
的相同错误

那么,编译器在使用
member_ptr2
的情况下做什么呢

额外问题:
const
测试之后是什么意思:*
?我注意到在编译器输出中

第二个额外问题:当使用函数指针时,
constepr const auto member_ptr1{…}
constepr auto member_ptr1{…}

static constexpr const auto member_ptr1{ &Test::Func }; 
声明指向成员函数的常量指针。这就是指针本身,它是常量

这个记录相当于

static constexpr void(Test::* const member_ptr2)(void) { &Test::Func };
这是一个演示程序

#include <iostream>
#include <iomanip>
#include <type_traits>

class Test
{
public:
    void Func(){}
};

static constexpr const auto member_ptr1{ &Test::Func }; // compile fine
static constexpr void(Test::* const member_ptr2)(void) { &Test::Func };

int main() 
{
    std::cout << std::boolalpha 
              << std::is_same<decltype( member_ptr1 ), decltype( member_ptr2 )>::value
              << '\n';
              
    return 0;
}
#include <iostream>

int main() 
{
    int x = 10;
    const auto p = &x;
    
    *p = 20;
    
    std::cout << "x = " << x << '\n';
    
    return 0;
}
这是另一个简化的演示程序

#include <iostream>
#include <iomanip>
#include <type_traits>

class Test
{
public:
    void Func(){}
};

static constexpr const auto member_ptr1{ &Test::Func }; // compile fine
static constexpr void(Test::* const member_ptr2)(void) { &Test::Func };

int main() 
{
    std::cout << std::boolalpha 
              << std::is_same<decltype( member_ptr1 ), decltype( member_ptr2 )>::value
              << '\n';
              
    return 0;
}
#include <iostream>

int main() 
{
    int x = 10;
    const auto p = &x;
    
    *p = 20;
    
    std::cout << "x = " << x << '\n';
    
    return 0;
}
也就是说,指针
p
的类型为
int*const
而不是
const-int*


取代占位符
auto
的类型首先从使用的初始值设定项中推导出来,然后将限定符const应用于推导出的类型。

请每个问题一个问题