为什么从const char*
到std::string
的隐式转换在后一种情况下不起作用? 请尽可能链接参考C++标准。数组初始值设定项列表中的隐式转换
变型1:
struct Foo {
Foo(const char* a) {}
};
int main() {
// works well for a "const char*" accepting constructor
Foo* foo = new Foo[1] { "a" };
}
变体2:
struct Foo {
Foo(std::string a) {}
};
int main() {
// could not convert from "const char*" to "Foo"
Foo* foo = new Foo[1] { "a" };
}
有没有这样的事情作为隐式演员。有一个cast(显式类型转换)和type coalescing(隐式类型转换),决定哪一个。 – 2013-04-12 10:21:02
检查如何命名的页面: http://en.cppreference.com/w/cpp/language/implicit_cast – Hertz