Bonjour,

ça faisait un certain temps que je n'avais pas posé une question ici, et cette nouvelle question est (comme la dernière il y a à peu près 1 an) lié à l'utilisation des rvalue reference. Un jour, je m'y ferait.

Soit le code suivant, qui ne fait rien d'utile, mais qui est suffisament proche dans sa conception pour provoquer la même erreur que j'ai dans mon vrai code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
#include <sstream>
#include <iostream>
 
struct bbb
{
	int n;
};
 
struct aaa
{
	std::stringstream m_stream;
	bbb& m_bobj;
 
	aaa(bbb& bobj)
	: m_bobj(bobj)
	{ }
	aaa(aaa&& other)
	: m_stream(std::move(other.m_stream))
	, m_bobj(other.m_bobj)
	{ }
	aaa& do_something()
	{ m_stream << "show " << m_bobj.n; return *this ; }
	void show()
	{ std::cout << m_stream.str() << std::endl; }
};
 
aaa make(bbb& bobj)
{
	aaa aobj(bobj);
	return aobj;
}
 
int main()
{
	bbb bobj = { 10 };
 
	make(bobj).do_something().show();
}
L'idée derrière ce code est que je ne peux pas créer un objet aaa de manière explicite - il est prévu pour n'être jamais vu (ou en tout cas utilisé) par le programmeur. L'instance est créée par un appel à une fonction, et elle n'est jamais manipulée directement.

Le problème est que ce type contient une référence ET un stream. Puisque l'instance est retournée par une fonction elle doit être soit copiable, soit moveable. Impossible qu'elle soit copiable (à cause de la référence ET du stream), j'opte donc pour un move ctor. Seulement, visiblement, j'ai des problèmes pour l'écrire...

En compilant avec g++ (la version n'a pas d'importance ; le problème se produit aussi bien avec la version 4.5 que la version 4.7), j'obtiens les erreurs suivantes :

show.cpp: In constructor ‘aaa::aaa(aaa&&)’:
show.cpp:19:23: error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’
In file included from show.cpp:1:0:
/usr/include/c++/4.7/sstream:483:11: note: ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.7/sstream:483:11: error: use of deleted function ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’
In file included from /usr/include/c++/4.7/sstream:39:0,
from show.cpp:1:
/usr/include/c++/4.7/istream:789:11: note: ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.7/istream:789:11: error: use of deleted function ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’
/usr/include/c++/4.7/istream:56:11: note: ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.7/istream:56:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from /usr/include/c++/4.7/ios:45:0,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/sstream:39,
from show.cpp:1:
/usr/include/c++/4.7/bits/basic_ios.h:64:11: note: ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ is implicitly deleted because the default definition would be ill-formed:
In file included from /usr/include/c++/4.7/ios:43:0,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/sstream:39,
from show.cpp:1:
/usr/include/c++/4.7/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
In file included from /usr/include/c++/4.7/ios:45:0,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/sstream:39,
from show.cpp:1:
/usr/include/c++/4.7/bits/basic_ios.h:64:11: error: within this context
In file included from /usr/include/c++/4.7/sstream:39:0,
from show.cpp:1:
/usr/include/c++/4.7/istream:56:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
/usr/include/c++/4.7/istream:789:11: error: use of deleted function ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’
In file included from /usr/include/c++/4.7/istream:41:0,
from /usr/include/c++/4.7/sstream:39,
from show.cpp:1:
/usr/include/c++/4.7/ostream:56:11: note: ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.7/ostream:56:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
/usr/include/c++/4.7/ostream:56:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from /usr/include/c++/4.7/sstream:39:0,
from show.cpp:1:
/usr/include/c++/4.7/istream:789:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from show.cpp:1:0:
/usr/include/c++/4.7/sstream:483:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
/usr/include/c++/4.7/sstream:483:11: error: use of deleted function ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’
/usr/include/c++/4.7/sstream:60:11: note: ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’ is implicitly deleted because the default definition would be ill-formed:
In file included from /usr/include/c++/4.7/ios:44:0,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/sstream:39,
from show.cpp:1:
/usr/include/c++/4.7/streambuf:800:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const __streambuf_type&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private
In file included from show.cpp:1:0:
/usr/include/c++/4.7/sstream:60:11: error: within this context
Qu'est ce que je fais de mal ?