【发布时间】:2014-12-19 21:10:26
【问题描述】:
以下代码可以使用 GCC 4.9.2 编译,但不能使用 Clang 3.5.0:
#include <string>
class Foo
{
public:
explicit operator std::string() const;
};
std::string bar{Foo{}}; // Works in g++, fails in clang++
std::string baz(Foo{}); // Works in both
clang++ 说:
foo.cpp:9:13: error: no matching constructor for initialization of 'std::string'
(aka 'basic_string<char>')
std::string bar{Foo{}};
^ ~~~~~~~
...: note: candidate constructor not viable: no known conversion from 'Foo' to
'const std::basic_string<char> &' for 1st argument
basic_string(const basic_string& __str);
^
奇怪的是,如果将 std::string 替换为像 int 这样的原始类型,它会起作用。
【问题讨论】:
-
gcc.gnu.org/bugzilla/show_bug.cgi?id=51553 和链接的 clang 非 bug 可能是相关的
标签: c++ c++11 gcc clang compiler-bug