Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using 'decltype' in the declaration of pointer to member valid?

Imagine for some strange reason I write this:

int main()
{
    struct S
    {        
        int i;
    } var;


    int decltype(var)::* pint = &decltype(var)::i;
}

GCC seems to compile it fine although Clang fails with some indeterminate syntax-related error message.

So what does the holy ISO paper says about this - is this valid or not?

like image 320
AnArrayOfFunctions Avatar asked Jul 03 '15 12:07

AnArrayOfFunctions


People also ask

What is the use of decltype?

The decltype type specifier yields the type of a specified expression. The decltype type specifier, together with the auto keyword, is useful primarily to developers who write template libraries. Use auto and decltype to declare a template function whose return type depends on the types of its template arguments.

Is decltype runtime or compile time?

decltype is a compile time evaluation (like sizeof ), and so can only use the static type.

How do you declare a pointer type?

The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated with a type (such as int and double) too. Naming Convention of Pointers: Include a "p" or "ptr" as prefix or suffix, e.g., iPtr, numberPtr, pNumber, pStudent.

How do I create a pointer to a member in C++?

The pointer to member operators . * and ->* are used to bind a pointer to a member of a specific class object. Because the precedence of () (function call operator) is higher than . * and ->* , you must use parentheses to call the function pointed to by ptf .


1 Answers

This is actually a known bug in Clang.

The code is valid.

N4140 [dcl.mptr]/1:

In a declaration T D where D has the form

nested-name-specifier * attribute-specifier-seqopt cv-qualifier-seqoptD1

and the nested-name-specifier denotes a class, and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is “derived-declarator-type-list cv-qualifier-seq pointer to member of class nested-name-specifier of type T”. The optional attribute-specifier-seq (7.6.1) appertains to the pointer-to-member.

In this definition we are interested in nested-name-specifier, and it's defined at [expr.prim.general]/8 as (emphasis mine):

nested-name-specifier:

:: type-name ::
namespace-name ::
decltype-specifier ::
nested-name-specifier identifier ::
nested-name-specifier templateopt simple-template-id ::

like image 119
Anton Savin Avatar answered Nov 15 '22 22:11

Anton Savin