2016-03-01 60 views
1

我試圖使用匿名ostringstream生成一個stringUse an Anonymous Stringstream to Construct a String做操縱器不知怎的轉換流類型?

然而,當我使用機械臂我似乎無法編譯下去:

const auto myString(static_cast<ostringstream>(ostringstream{} << setfill('!') << setw(13) << "lorem ipsum").str()); 

但是,這似乎並沒有不允許even in gcc 5.1

prog.cpp: In function int main() :
prog.cpp:8:109: error: no matching function for call to std::basic_ostringstream<char>::basic_ostringstream(std::basic_ostream<char>&)
const auto myString(static_cast<ostringstream>(ostringstream{} << setfill('!') << setw(13) << "lorem ipsum").str());


In file included from /usr/include/c++/5/iomanip:45:0,
from prog.cpp:1:
/usr/include/c++/5/sstream:582:7: note: candidate
std::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(std::basic_ostringstream<_CharT, _Traits, _Alloc>&&) [with _CharT = char ; _Traits = std::char_traits<char> ; _Alloc = std::allocator<char> ]
basic_ostringstream(basic_ostringstream&& __rhs)

/usr/include/c++/5/sstream:582:7: note: no known conversion for argument 1 from std::basic_ostream<char> to std::basic_ostringstream<char>&&
/usr/include/c++/5/sstream:565:7: note: candidate:
std::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(const __string_type&, std::ios_base::openmode) [with _CharT = char ; _Traits = std::char_traits<char> ; _Alloc = std::allocator<char> ; std::basic_ostringstream<_CharT, _Traits, _Alloc>::__string_type = std::basic_string<char> ; std::ios_base::openmode = std::_Ios_Openmode ]
basic_ostringstream(const __string_type& __str,

/usr/include/c++/5/sstream:565:7: note: no known conversion for argument 1 from std::basic_ostream<char> to const __string_type& {aka const std::basic_string<char>&}
/usr/include/c++/5/sstream:547:7: note: candidate:
std::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(std::ios_base::openmode) [with _CharT = char ; _Traits = std::char_traits<char> ; _Alloc = std::allocator<char> ; std::ios_base::openmode = std::_Ios_Openmode ]
basic_ostringstream(ios_base::openmode __mode = ios_base::out)

/usr/include/c++/5/sstream:547:7: note: no known conversion for argument 1 from std::basic_ostream<char> to std::ios_base::openmode {aka std::_Ios_Openmode}

這是另一GCC流錯誤,或者是我在做什麼實際上是非法?

回答

3
static_cast<ostringstream>(...) 

這將着力構建從參數新ostringstream在括號,一個std::ostream&,對此有沒有std::ostringstream構造。

您只想將參考轉換回原始類型。使演員參考:

static_cast<ostringstream&>(...) 

然後它工作正常。

我不知道你的想法是什麼,但忽略了參考,並刪除操縱器,它仍然失敗。

+0

那麼現在我覺得很笨。感謝您捕捉我缺少的參考。 –