Opened 12 years ago

Last modified 10 years ago

#5483 new Feature Requests

value_init: Assignment to 'value_initialized', 'initialized' objects doesn't work automatically

Reported by: Christian Masloch <cm@…> Owned by: No-Maintainer
Milestone: To Be Determined Component: utility
Version: Boost 1.46.1 Severity: Problem
Keywords: Cc:

Description

Objects of the 'initialized<T>' class (and presumably 'value_initialized<T>' too) do not automatically convert to assignment lvalues and do not provide proper assignment operator methods either. Assignments therefore do not automatically work, for example, declaring 'initialized<int> i;' then writing 'i = 1;'.

Tested on Windows XP SP3 with MinGW g++ 4.4.1, as well as the (VS 2008) Visual C++ 9.0 compiler. Both compilers report that no match for operator= can be found in that assignment, and that the method 'initialized<int>& initialized<int>::operator=(initialized<int> const&)' is considered as 'candidate' that would fit with a conversion inbetween (from 'int' to 'initialized<int>'), but it's not being used. Conversion operators such as the one defined as 'initialized<int>::operator int&()' apparently do not automatically qualify for lvalues either.

Workarounds specify the necessary conversion or operator method, for example, 'i.operator int&() = 1;' and 'get(i) = 1;' both work.

The attached file contains some tests as well as a class implementing a simplified version of the interface of 'initialized<T>' with a fix. The file also contains the error message from g++. I omitted the one from Visual C++ (because I would have to translate it into English) but it essentially says the same.

Attachments (2)

initialized-assignment-test.cpp (2.1 KB ) - added by Christian Masloch <cm@…> 12 years ago.
initialized-assignment-fix.patch (1.4 KB ) - added by Christian Masloch <cm@…> 12 years ago.
Patch that adds operator=(<T>const&) to initialized, as well as operator=(<T>const&) and operator=(value_initialized const&) to value_initialized.

Download all attachments as: .zip

Change History (4)

by Christian Masloch <cm@…>, 12 years ago

comment:1 by Christian Masloch <cm@…>, 12 years ago

Oh! Adding a constructor from int (ie T) that is not declared 'explicit' resolves this without the additional assignment operator that I requested. But since that constructor (for 'initialized<T>') is declared as 'explicit', the core issue is still to be solved here.

by Christian Masloch <cm@…>, 12 years ago

Patch that adds operator=(<T>const&) to initialized, as well as operator=(<T>const&) and operator=(value_initialized const&) to value_initialized.

comment:2 by viboes, 10 years ago

Type: BugsFeature Requests

Moved to feature request as I don't see any problem with the current implementation.

Note: See TracTickets for help on using tickets.