【发布时间】:2015-05-26 14:34:47
【问题描述】:
代码如下:
#include <iostream>
#include <string>
using namespace std;
class Foo {
public:
operator string() const { return n; }
string n {"foo"};
};
int main (int argc, char** argv) {
string s {Foo{}};
cout << s << endl;
return 0;
}
这段代码使用 gcc 4.8.3 编译,但它没有使用 clang 3.5 编译,有人能告诉我它有什么问题吗?
我收到这样的错误:
main.cpp:45:12: error: no matching constructor for initialization of 'string' (aka 'basic_string<char>')
string s {Foo{}};
^ ~~~~~~~
clang --version:
clang version 3.5.0 (tags/RELEASE_350/final 216961)
Target: x86_64-suse-linux
Thread model: posix
谢谢
【问题讨论】:
-
一个明显的可能性:你没有包含
<string>(而 gcc 恰好从你包含的其他一些标头中包含<string>)。 -
如果我们能获得完整的文件(包括所有文件)以减少在解释数据时出现人为错误的可能性,那就太好了。