0

In the C++17 standard draft document, in subsection (6.7.3) "CV-qualifiers" [basic.type.qualifier]: paragraph 6, there is this sentence:

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

I have C language background and was randomly reading C++ standard and can't make sens of this, I tried to interpret it as

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)

does someone have an explanation ?

I have tried this code

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);
            ^

changing to what is adviced doesn't work !

thank you for your help

dbush
  • 205,898
  • 23
  • 218
  • 273
  • First part, constant member functions are explained in any C++ tutorial. `volatile` is a bit more complicated, but any object, just like it can be `const`, can also be `volatile`. Even if that object is a pointer to a member function. – Ulrich Eckhardt Dec 19 '19 at 21:32
  • I understand what are const and volatile, my question is about the type-id used in the example to illustrate top-level qualifier, and I can't make sens of this type-id the `C:: ` part especially – walid TALABOULMA Dec 19 '19 at 21:58

1 Answers1

1

The C::* syntax denotes a member-function pointer which means a pointer to a member-function (sometimes called a method) that belongs to a class C.

In order to obtain an address of a member-function you need to use special syntax with qualified member-function name:

X a = &C::f;

In order to call a member-function via pointer you need to use a special operator .* (as your compiler advises):

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

Note that we need to use t twice: for accessing a member and for calling a with t as this.

r3mus n0x
  • 5,954
  • 1
  • 13
  • 34