C++标准中短语"constructors do not have names"的含义
Meaning of phrase "constructors do not have names" in the C++ Standard
在试图理解C++标准中的短语"构造函数没有名称"时,我似乎在clang中发现了一个错误。有人能证实吗?
VS2015
和gcc
拒绝此代码,我认为他们它是是正确的。至少,这是我从N4140:中的§12.1[class.ctor]/2中得到的印象
#include <iostream>
class A {
public:
A() { std::cout << "A()" << 'n'; }
};
int main()
{
A::A();
}
N4140:中的§12.1[class.ctor]/2
构造函数用于初始化其类类型的对象。因为构造函数没有名称,在名称过程中永远找不到它们查找。。。
对于上面的表达式A::A();
,clang通过名称查找来查找构造函数,而应该查找类型名称A
。请参阅实例。
你的直觉是正确的。这是一个已知的Clang bug 13403,状态为NEW
。
我同意这不应该编译。
事实上,这比你想象的还要奇怪。试试这个:
#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();
}
示例输出:
A() 0x7fff5cd105f8
hello
在我看来,似乎在隐式地创建一个未命名的A。
相关文章:
- 为什么static_assert错误:即使我传递常量"expression must have a constant value"?
- 错误:"Left of getValue must have class/struct/union"
- 修复完成输入后重复"enter names of teacher"?
- unordered_set .find 方法无法编译的 C++ "expression must have class type"错误
- 编译为 cuda 内核调用提供了"expression must have integral or unscoped enum type"
- 警告的原因是什么:"when type is in parentheses, array cannot have dynamic size"?
- 调用 list.size() 方法给出错误"expression must have class type"
- 为什么这段代码抱怨"a non-type template parameter cannot have type"?
- 模板元编程:"does not have integral or enumeration type"
- G++ 4.4.7 中的"names the constructor, not the type"
- 为什么我会收到"Expression must have a class type"错误?(C++)
- C++ 中数组的"expression must have pointer-to-object type"是什么?
- 当值不能恒定时如何修复"Expression must have a constant value"?
- 仅当链接函数 C++ 时,才会发生"Expression must have class type error"
- 我可以忽略"Intellisense: (E0028) expression must have a constant value"吗?
- 得到错误"left of '.toString' must have class/struct/union",即使 toString 应该有一个合适的对象
- "does class T have member X":已定义类模板
- 字符串初始化失败:"expression must have integral or unscoped enum type"
- Does xgboost have feature_importances_?
- C++标准中短语"constructors do not have names"的含义