1

ostringstream匿名を使用して生成しようとしていますstring:匿名の文字列ストリームを使用して文字列を構築します

ただし、マニピュレータを使用すると、コンパイルできなくなったようです。

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

しかし、それはgcc 5.1 でも許可されていないようです:

prog.cpp: 関数内int main():
prog.cpp:8:109: エラー: std::basic_ostringstream<char>::basic_ostringstream(std::basic_ostream<char>&)
const auto myString(static_cast<ostringstream>(ostringstream{} << setfill('!') << setw(13) << "lorem ipsum").str());


/usr/include/c++/5/iomanip:45:0 から含まれるファイル内の呼び出しに一致する関数がありません
: prog.cpp:1 から:
/ usr/include/c++/5/sstream:582:7: 注: 候補
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: 注: 引数 1 から /usr/include/c++/5/sstream:565:7 への既知の変換はありませんstd::basic_ostream<char>:std::basic_ostringstream<char>&&
注: 候補:
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: 注: 引数 1 から /usr/include/c++/5/sstream:547:7 への既知の変換はありませんstd::basic_ostream<char>:const __string_type& {aka const std::basic_string<char>&}
注: 候補:
std::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(std::ios_base::openmode)[_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: 注: 引数 1 からstd::basic_ostream<char>への既知の変換はありませんstd::ios_base::openmode {aka std::_Ios_Openmode}

これは別の gcc ストリームのバグですか、それとも私がしていることは実際には違法ですか?

4

1 に答える 1

3
static_cast<ostringstream>(...)

ostringstreamこれは、括弧内の引数a から new を構築しようとしますがstd::ostream&、 のコンストラクターはありませんstd::ostringstream

参照を元の型にキャストしたいだけです。参照へのキャストを行います。

static_cast<ostringstream&>(...)

その後、正常に動作します。

何がうまくいったと思ったのかわかりませんが、参照を省略してマニピュレータを削除しても、まだ失敗します。

于 2016-03-01T19:21:15.273 に答える