Wal*_*ter 2 c++ gcc type-conversion clang c++11
这个简单的代码
bool foo(std::istringstream&stream, std::string&single, char del)
{ return std::getline(stream,single,del); }
Run Code Online (Sandbox Code Playgroud)
用gcc(4.8.2),但不与铛编译(3.4,使用的libc ++),其抱怨有从没有可行转换std::basic_istream<char, std::char_traits<char> >到bool.但是,当我将参数包装到一个return语句中时static_cast<bool>(),clang很高兴.
这让我感到困惑让我想知道上面的代码是否形成良好,即gcc或clang是否正确.根据cpprefernce std::getline返回a std::basic_istream<char, std::char_traits<char> >,它继承自std::basic_ios具有类型转换的operator bool(因为C++ 11,在它进行类型转换之前void*).不应该自动选择此转换运算符吗?(出于某种原因,我更愿意接受gcc错误而不是clang).
编辑我刚才发现显然llvm的libc ++声明了有问题的转换运算符explicit,认为它对隐式转换无效.这符合标准吗?
铿锵是对的.由于C++ 11,从转换std::basic_ios到bool确实需要是explicit.
C++ 11 [ios.overview],:
explicit operator bool() const;
Run Code Online (Sandbox Code Playgroud)