问题
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.
回答1:
Your intuition is correct. This is a known Clang bug 13403 with status NEW
.
回答2:
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.
来源:https://stackoverflow.com/questions/33842988/meaning-of-phrase-constructors-do-not-have-names-in-the-c-standard