简体   繁体   中英

std::add_pointer implementation for non-static member functions

This question is a follow-up of A question regarding the implementation of std::add_pointer

Under std::add_pointer

there is the following reference:

Otherwise (if T is a cv- or ref-qualified function type), provides the member typedef type which is the type T.

Based on reading Non-static member functions: const-, volatile-, and ref-qualified member functions , my understanding is that a for a non-static member function with given cv and/or ref qualification,

a) the cv qualification of the function applies to the this pointer as well, within the scope of the function

b) the ref qualification of the function does not apply to the this pointer within the scope of the function

Given this, why is it that std::add_pointer cannot provide the member typedef type T* in the case of a non-static member function with cv or ref qualification?

Per [dcl.ptr]/4 :

[ Note: Forming a pointer to reference type is ill-formed; see [dcl.ref]. Forming a function pointer type is ill-formed if the function type has cv-qualifiers or a ref-qualifier ; see [dcl.fct]. Since the address of a bit-field cannot be taken, a pointer can never point to a bit-field. end note ]

The pointer-to-cv-qualified-function type you are imagining is actually nonexistent. Therefore, std::add_pointer cannot produce such a type :)

A non-static member function type cannot be formed. Such a thing does not exist.

struct T {
   int func() const;
};

func does not have a type. You cannot ever use it as an expression on its own.

using mf = int (T::*)() const; 
mf myfunc = &T::func;

mf is a pointer to member function type. It is not a function type; pointers to non-static members (including member functions) are data, not functions.

A cv or ref qualification on a member function does not qalify the function type, which does not exist, but the type of the implicit "this" parameter.

The paragraph you quote only applies to non-member or static member functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM