Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of phrase "constructors do not have names" in the C++ Standard

Tags:

While trying to understand the phrase "constructors do not have names" in the C++ Standard, it seems like I found an error in clang. Could someone confirm this?

VS2015 and gcc rejects this code, and I think they it are is correct. At least, this is the impression I get from §12.1[class.ctor]/2 in N4140:

#include <iostream> class A { public:     A() { std::cout << "A()" << '\n'; } };  int main() {   A::A(); } 

§12.1[class.ctor]/2 in N4140:

A constructor is used to initialize objects of its class type. Because constructors do not have names, they are never found during name lookup; ...

With the expression A::A(); above, clang finds the constructor by name lookup, when it should find the type name A instead. See live example.

like image 959
Belloc Avatar asked Nov 21 '15 11:11

Belloc


2 Answers

Your intuition is correct. This is a known Clang bug 13403 with status NEW.

like image 188
101010 Avatar answered Sep 28 '22 11:09

101010


I agree that this should not compile.

It's actually more bizzare than you thought. Try this:

#include <iostream> #include <string>  class A { public:     A() {         std::cout << "A() " << this << '\n';     }      void foo() {         std::cout << _message << std::endl;     }      std::string _message = "hello"; };  int main() {     A::A().foo(); } 

example output:

A() 0x7fff5cd105f8 hello 

It looks to me as if an un-named A is being implicitly created.

like image 30
Richard Hodges Avatar answered Sep 28 '22 10:09

Richard Hodges