3

According to Godbolt, this code compiles with MSVC but not with GCC and Clang [conformance view]. Which one is right and why?

#include <iostream>

void Example (){
    std::cout << "function";
}

class Example{ 
    public: Example() { 
        std::cout << "constructor";
    }
};

int main()
{
    Example();
    class Example();
}

I understand that the function will be preferred, which is why I wrote class in the second line.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222

1 Answers1

3

I believe MSVC is not standard-conforming in this case. Elaborated type specifiers are not allowed in functional-style cast expressions.

Expressions in the form T() are called functional-style cast expressions. [expr.type.conv]/1 gives its precise syntax:

A simple-type-specifier or typename-specifier followed by a parenthesized optional expression-list or by a braced-init-list

Sadly neither simple-type-specifier nor typename-specifier allow elaborated type specifiers so class T() is illegal.

cppreference has a more comprehensible explanation: T must be a single word type name (with optional qualification and template arguments). int(), std::string(), std::vector<int>() are OK, while unsigned int(), class std::vector<int>() are not OK.

Lancern
  • 403
  • 2
  • 9