无法理解 C++ 参考手册中示例中使用的这种类型 `void(C::* volatile)(int) const `

can not make sens of this type `void(C::* volatile)(int) const ` used in an example in C++ reference manual

在 C++17 标准草案文档中,在 (6.7.3) "CV-qualifiers" [basic.type.qualifier] 小节中: 第6段,有这句话:

For a class type C, the type corresponding to the type-id void(C::* volatile)(int) const has the top-level cv-qualifier volatile.

我有 C 语言背景,随机阅读 C++ 标准,无法理解这一点,我试图将其解释为

a volatile function pointer to a function that can be called on const objects and that takes a single int argument and return a ??? ( C type / void I am lost here)

有人有解释吗?

我试过这个代码

int main()
{
    class C 
    {
       public:
       typedef void(C:: * volatile X)(int)const ;
       void f(int z) const 
            {cout << z << endl;} 

       X a = (X)&f;  // had to add -fpermissive flag
    };

    C t;
    t.f(5); // works obviously
    (t.a)(5); // gives the following compilation message

main.cpp:22:18: warning: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say ‘&main()::C::f’ [-fpermissive]
        X a = (X)&f;
                  ^
main.cpp:27:12: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘t.main()::C::a (...)’, e.g. ‘(... ->* t.main()::C::a) (...)’
     (t.a)(5);
            ^

更改为建议的内容无效!

感谢您的帮助

C::*语法表示一个成员函数指针,它表示指向属于[=30的成员函数(有时称为方法)的指针=] C.

为了获得成员函数的地址,您需要使用具有限定成员函数名称的特殊语法:

X a = &C::f;

为了通过指针调用成员函数,您需要使用特殊运算符 .*(如您的编译器所建议的那样):

(t.*(t.a))(5);

请注意,我们需要使用 t 两次:用于访问 a 成员和使用 t 作为 this 调用 a