2016-03-01 2 views
1

Je suis en train d'utiliser un anonyme ostringstream pour générer un string: Use an Anonymous Stringstream to Construct a StringEst-ce que les manipulateurs convertissent le type de flux d'une manière ou d'une autre?

Cependant quand j'utilise Manipulateurs je ne peux pas sembler compiler plus longtemps:

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

Mais cela ne semble pas être autorisé 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}

est-ce un autre bug de flux gcc, ou est ce que je fais en fait illégal?

Répondre

3
static_cast<ostringstream>(...) 

Cela va essayer de construire une nouvelle ostringstream de l'argument des parens, un std::ostream&, pour lequel il n'y a pas de constructeur std::ostringstream.

Vous souhaitez simplement renvoyer la référence au type d'origine. Faire le casting à une référence:

static_cast<ostringstream&>(...) 

Ensuite, cela fonctionne très bien.

Je ne sais pas ce que vous pensiez fonctionner, mais en omettant la référence, et en enlevant les manipulateurs, ça échoue toujours.

+0

Eh bien maintenant je me sens bête. Merci d'avoir rattrapé ma référence manquante. –