为什么无法正确分析显式析构函数调用中的限定类型名称?
Why can't a qualified type name in an explicit destructor call be parsed correctly?
考虑一个例子。
#include <string>
struct S {
S() {
new (&s) std::string("hi");
}
~S() {
// does not compile
// s.~std::string();
// compiles
using std::string;
s.~string();
}
union {
std::string s;
};
};
为什么注释掉的部分无法编译?
我从 clang 收到的错误消息显示编译器将std
本身解析为一种类型。
对象销毁表达式中的标识符"std"未命名类型
为什么编译器无法确定std::string
是类型?这在某种程度上是模棱两可的吗?
我从安德烈·亚历山德雷斯库的演讲中意识到了这一点。现在是37:10。他很快评论说,如果类型名称是限定的,这"不会解析",但没有解释原因。
http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C
(我使用"parse"这个词主要是因为他这样做了,我没有想到更好的词。不要读得太深。我并不是说编译器做错了什么。
原因是std::string
实际上并没有命名类类型。 除此之外,这意味着不存在名为~string()
的析构函数。
std::string
,在<string>
中,实际上是一个 typedef(替代名称),用于名为 std::basic_string<char>
的模板类类型的专用化。
typedef some_class_name another_name
形式的typedef
不会导致存在名为 ~another_name
的析构函数。
相关文章:
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 类型擦除的std::function与虚拟函数调用的开销
- 避免在人为的重载函数调用中拼写出类型
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 没有使用两种类型的字符串进行匹配的函数调用
- 为什么此函数通过类型函数指针调用后,呼叫明智地行为
- 如何使用无类型函数指针调用C++成员函数
- 为什么函数调用是xvalue(如果返回类型为rvalue)
- GCC 9.1 返回 void& 作为显式析构函数调用的结果类型。这是一个错误吗?
- 模板函数调用中C++错误:不允许使用类型名称
- 使用variadic模板进行函数调用的模板类型扣除
- 在函数调用中使用类型vector<pair<int,int>>::iterator&in
- 数组类型在函数调用中用作引用类型参数
- 函数调用之前,C 类型声明
- 类型强制转换指针构造函数调用
- 为什么 C++ 和 Java 中的构造函数调用需要显式类型参数
- 使用子类型重载调用函数
- 对未解析的重载函数类型的调用没有匹配的函数
- 使用可变包类型扩展的C++函数调用程序包装
- 为什么无法正确分析显式析构函数调用中的限定类型名称?