mirror of
https://github.com/boostorg/optional.git
synced 2025-08-03 22:44:27 +02:00
Fix: prevented the binding illegal temporary to optional<const int&>
Older MSVC versions add illegal temporary when you want to assign from const integral value.
This commit is contained in:
@@ -30,17 +30,17 @@ The implementation uses the following other Boost modules:
|
||||
|
||||
[section Optional Reference Binding]
|
||||
|
||||
On compilers that do not conform to Standard C++ rules of reference binding, operations on optional references might give adverse results: rather than binding a reference to a designated object they may create an unexpected temporary and bind to it. Compilers known to have these deficiencies include GCC versions 4.2, 4.3, 4.4, 4.5; QCC 4.4.2; MSVC versions 8.0, 9.0, 10.0, 11.0, 12.0. On these compilers prefer using direct-initialization and copy assignment of optional references to copy-initialization and assignment from `T&`:
|
||||
A number of compilers incorrectly treat const lvalues of integral type as rvalues, and create an illegal temporary when binding to an lvalue reference to const in some expressions. This could result in creating an optional lvalue reference that is in fact bound to an unexpected temporary rather than to the intended object. In order to prevent hard to find run-time bugs, this library performs compile-time checks to prevent expressions that would otherwise bind an optional reference to an unexpected temporary. As a consequence, on certain compilers certain pieces of functionality in optional references are missing. In order to maintain a portability of your code across diferent compilers, it is recommended that you only stick to the minimum portable interface of optional references: prefer direct-initialization and copy assignment of optional references to copy-initialization and assignment from `T&`:
|
||||
|
||||
const int i = 0;
|
||||
optional<const int&> or1;
|
||||
optional<const int&> or2 = i; // not portable
|
||||
or1 = i; // not portable
|
||||
optional<const int&> or2 = i; // caution: not portable
|
||||
or1 = i; // caution: not portable
|
||||
|
||||
optional<const int&> or3(i); // portable
|
||||
or1 = optional<const int&>(i); // portable
|
||||
|
||||
In order to check if your compiler correctly implements reference binding use this test program.
|
||||
Compilers known to have these deficiencies include GCC versions 4.2, 4.3, 4.4, 4.5, 5.1, 5.2; QCC 4.4.2; MSVC versions 8.0, 9.0, 10.0, 11.0, 12.0. In order to check if your compiler correctly implements reference binding use this test program.
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
Reference in New Issue
Block a user