Mac gcc不允许显式调用std :: string ::〜字符串

Yas*_*smi 9 c++ macos

strdata->std::string::~string();    
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

error: '~' in destructor name should be after nested name specifier
        strdata->std::string::~string();
                              ^
Run Code Online (Sandbox Code Playgroud)

我正在使用cmake项目...我通过brew安装的gcc版本如下:

gcc --version配置: - prefix =/Library/Developer/CommandLineTools/usr --with-gxx-include-dir =/usr/include/c ++/4.2.1 Apple LLVM 7.0.2版(clang-700.1. 81)目标:x86_64-apple-darwin15.2.0线程模型:posix

我找不到在头文件中任何地方定义的~string().我最终改变它如下,这是有效的.现在可以使用我的用例了.

strdata->std::string::~basic_string();
Run Code Online (Sandbox Code Playgroud)

这个原始版本似乎是正确的,并且在Linux和CYGWIN的GCC中完美运行.阻止它在mac上运行的问题是什么?模板?别的什么?

Cor*_*lks 2

这不是一个完整的答案。由于某种原因,using namespace std;可以工作,但如果没有的话clang就会失败。考虑这个例子:

#include <new>
#include <type_traits>

namespace foo {

struct A {};
typedef A A_t;

}

int main() {
    std::aligned_storage<sizeof(foo::A)>::type storage;

    foo::A_t* obj = new(&storage) foo::A;

    using namespace foo; // Without this line, clang fails.
    obj->foo::A_t::~A_t();
}
Run Code Online (Sandbox Code Playgroud)

如果没有这using namespace foo;条线,clang 会报错expected the class name after '~' to name a destructor。但有了这条线,它就可以工作了。将其扩展为std::string

#include <new>
#include <type_traits>
#include <string>

int main() {
    std::aligned_storage<sizeof(std::string)>::type storage;

    std::string* obj = new(&storage) std::string;

    using namespace std; // Without this line, clang fails.
    obj->std::string::~string();
}
Run Code Online (Sandbox Code Playgroud)

有用。它也适用于较窄的using std::string;.

这并没有回答为什么 失败的问题clang。不知道是bugclang还是gcc. 但至少存在一种解决方法。

可能值得将其报告为 中的错误clang,然后让他们决定它是否确实是错误。