Opened 13 years ago
Closed 12 years ago
#3395 closed Bugs (fixed)
Assignment to optional is not possible if operator& is overriden
Reported by: | Andrey Semashev | Owned by: | Fernando Cacciola |
---|---|---|---|
Milestone: | Boost 1.41.0 | Component: | optional |
Version: | Boost 1.40.0 | Severity: | Problem |
Keywords: | optional assignment | Cc: |
Description
The assignment operator for optional< T > uses T::operator& to dispatch the call between assign_expr functions. This dispatch breaks if the T::operator& returns something else than T*. This is the case with ATL types, such as CComBSTR.
The suggested fix is to replace call to operator& with addressof. Please, find the attached patch against the 1.40 release.
Attachments (2)
Change History (12)
comment:1 by , 13 years ago
by , 13 years ago
Attachment: | optional.hpp.patch added |
---|
An updated fix that also optimises away copying the assignment argument
follow-up: 3 comment:2 by , 12 years ago
follow-up: 4 comment:3 by , 12 years ago
The current optional.hpp leads to gcc 3.2 warnings (Redhat 8.0). It is easily fixed with the proposed patch I attached a minute ago. Is this OK to commit? -- It is tested already locally on a large number of platforms.
comment:4 by , 12 years ago
Replying to rwgk:
The current optional.hpp leads to gcc 3.2 warnings (Redhat 8.0). It is easily fixed with the proposed patch I attached a minute ago. Is this OK to commit? -- It is tested already locally on a large number of platforms.
This looks good to me.
comment:6 by , 12 years ago
Perhaps, the patch should also take into account GCC versions prior to 3.2? I really don't know since which version may_alias has become available.
comment:7 by , 12 years ago
I checked in my patch as svn trunk rev. [67109] before seeing comment 6.
Is anyone still using a gcc before 3.2? Even 3.2 is ancient now (released 2002). It is the oldest gcc I have, i.e. I couldn't test with any older versions.
comment:8 by , 12 years ago
That's ok, I've updated your change with a check for earlier GCC versions. Try with the updated version.
comment:10 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
(In [67183]) Merged changes from trunk. Fixes #3395. Also updates swap behavior: if default constructor has no-throw guarantee, swap will use it to provide no-throw guarantee itself. operator>> behavior changed slightly so that the stream is not accessed when unrecognized character sequence is detected. The stream is marked with failbit in such a case.
BTW, I just now noticed that the assignment operator accepts its argument by value. It may be worth to accept it by const reference, as the operator is not only called for in-place factories but also for other types. For example:
I'm attaching an updated patch that fixes this, too.