Poniższy kod kompiluje z GCC 4.9.2, ale nie z Clang 3.5.0:Czy jawne operatory konwersji są dozwolone na listach inicjalizujących ze wzmocnionymi liniami?
#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
brzękiem ++ mówi:
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);
^
Co ciekawe, to działa, jeśli std::string
otrzymuje z prymitywnego typu jak int
.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51553 i związane dzyń non-bug są prawdopodobnie istotne –