| 1 |
|
|---|
| 2 | /*
|
|---|
| 3 | This is gcc's output with all #defines (below) defined, showing the error
|
|---|
| 4 | caused by using an object of type boost::initialized<int> as lvalue.
|
|---|
| 5 | This can be fixed by implementing the method
|
|---|
| 6 | boost::initialized<T>& boost::initialized<T>::operator=(T const&)
|
|---|
| 7 | for the initialized<T> class.
|
|---|
| 8 |
|
|---|
| 9 | File Line Message
|
|---|
| 10 | main.cpp In function 'int main(int, char**)':
|
|---|
| 11 | main.cpp 57 error: no match for 'operator=' in 'b = 39'
|
|---|
| 12 | boost/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>
|
|---|
| 20 | using std::cout;
|
|---|
| 21 | using std::endl;
|
|---|
| 22 |
|
|---|
| 23 | #include <boost/utility/value_init.hpp>
|
|---|
| 24 | using boost::initialized;
|
|---|
| 25 | using boost::get;
|
|---|
| 26 |
|
|---|
| 27 | class foo {
|
|---|
| 28 | public:
|
|---|
| 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 |
|
|---|
| 54 | int 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 | }
|
|---|