为什么无法正确分析显式析构函数调用中的限定类型名称?

Why can't a qualified type name in an explicit destructor call be parsed correctly?

本文关键字:类型 函数调用 析构 为什么      更新时间:2023-10-16

考虑一个例子。

#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 的析构函数。