Ticket #5483: initialized-assignment-test.cpp

File initialized-assignment-test.cpp, 2.1 KB (added by Christian Masloch <cm@…>, 12 years ago)
Line 
1
2/*
3This is gcc's output with all #defines (below) defined, showing the error
4caused by using an object of type boost::initialized<int> as lvalue.
5This can be fixed by implementing the method
6 boost::initialized<T>& boost::initialized<T>::operator=(T const&)
7for the initialized<T> class.
8
9File Line Message
10main.cpp In function 'int main(int, char**)':
11main.cpp 57 error: no match for 'operator=' in 'b = 39'
12boost/utility/value_init.hpp 120 note: candidates are: boost::initialized<T>& boost::initialized<T>::operator=(const boost::initialized<T>&) [with T = int]
13*/
14
15#define INITIALIZED_FAILURE
16#define FOO_FAILURE
17#define FOO_ASSIGNMENT_OPERATOR_FROM_INT
18
19#include <iostream>
20using std::cout;
21using std::endl;
22
23#include <boost/utility/value_init.hpp>
24using boost::initialized;
25using boost::get;
26
27class foo {
28public:
29 int x;
30
31 foo() : x(38) {}
32
33 operator int& () {
34 return x;
35 }
36
37 operator int const& () const {
38 return x;
39 }
40
41#ifdef FOO_ASSIGNMENT_OPERATOR_FROM_INT
42 foo& operator=(int const& o) {
43 x = o;
44 return *this;
45 }
46#endif
47
48 foo& operator=(foo const& o) { // if foo& foo::operator=(int const&) is undefined, this one is considered a "candidate" for the lvalue but not used
49 x = o;
50 return *this;
51 }
52};
53
54int main(int, char**) {
55 foo f;
56 cout << endl << f;
57#ifdef FOO_FAILURE
58 f = 39; // succeeds if foo& foo::operator(int const&) defined, fails otherwise
59 cout << endl << f;
60#endif
61 f.operator int&() = 40; // succeeds
62 cout << endl << f;
63 cout << endl;
64
65 initialized<int> b;
66 cout << endl << b;
67#ifdef INITIALIZED_FAILURE
68 b = 39; // fails (with initialized<int>& initialized<int>::operator=(initialized<int> const&) considered as above)
69 cout << endl << b;
70#endif
71 b.operator int&() = 40; // succeeds
72 cout << endl << b;
73 get(b) = 41; // succeeds
74 cout << endl << b;
75 cout << endl;
76
77 return 0;
78}