为什么不能正确解析显式析构函数调用中的限定类型名称?

Pra*_*tic 5 c++ destructor placement-new

考虑一个例子。

#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;
    };
};
Run Code Online (Sandbox Code Playgroud)

为什么注释掉的部分不能编译?

我从 clang 得到的错误消息表明编译器将std自身解析为一种类型。

对象销毁表达式中的标识符“std”未命名类型

为什么编译器不能确定这std::string是类型?这在某种程度上是模棱两可的吗?

我从 Andrei Alexandrescu 的演讲中了解到这一点。现在是 37:10。他很快评论说,如果类型名称是合格的,这“不会解析”,但没有解释原因。

http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C

(我使用“解析”这个词主要是因为他这样做了,我没有想到更好的词。不要读得太深。我并不是说编译器做错了什么。)

Pet*_*ter 0

原因是它std::string 实际上并没有命名类类型。除此之外,这意味着不~string()存在名为的析构函数。

std::string在 中,实际上是<string>一个 typedef (替代名称),用于名为 的模板类类型的特化std::basic_string<char>

typedef形式的Atypedef some_class_name another_name不会导致名为 的析构函数存在~another_name