Refactored the use of ref_t. Also fixes a crash. #146
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was quite a mistake in the design of
ref_t
solution.It was initially designed to make the arguments for a function call, that had to be references, passed with an explicit marking that these are references, not values, and not just pointers. The solution initially was done with the use of a C++11 type,
std::reference_wrapper
, so that the standardstd::ref
function can be used as the aforementioned marker, however this has been later changed in the standard to be able to accept a reference in a non-explicit constructor, which completely broke the intention. Therefore a new type has been defined,ref_t
, which in C++11 version only derives fromstd::reference_wrapper
, but applies also appropriate fixes. Still, due to interoperability problems between C++03 compiled SRT and C++11 compiled applications, the halfway solution was still to create a customRef
function as a helper, which in C++11 version resolves tostd::ref
, but in C++03 version it's a custom function, and soref_t
is a custom class, although based on the sources ofstd::reference_wrapper
.The problematic intention of the
std::reference_wrapper
, however, is that the assignment operator is intended to rebind the reference, rather than assign a value. It has been a design flaw then to add an assignment operator for a value for theref_t
type because in this case the form of assignment to aref_t
variable from a designated type value and from anotherref_t
variable were two completely different and unrelated operations, while the second one did not copy the value, but rebound the reference instead.Because of that, the design of
ref_t
has been changed so that the direct access to the designated reference is no longer implicitly available. It was earlier designed to use theget()
method to access the designated reference, and so it remains, but theoperator Type&()
function has been removed (in C++11 version the derived method has been deleted). For convenience, theoperator*
method has been added so that the reference can be still obtained easily, but still also explicitly. This makes the whole solution look more like a pointer, but still as it is made impossible to create aref_t
value with no argument, it is ensured enough that a null pointer can't be passed byref_t
(or, better said, to make a null pointer passed here one must do it very intentionally and explicitly). There was also left the overloaded theoperator->
function, which allows an easier access to the pointer value, in case when theref_t
type variable wraps a pointer variable - this has been stated as not dangerous, as it still doesn't give a direct access to modifying theref_t
variable itself. Might be further discussed, whether the implicit returning a value copy of the designated reference has some reason; currently it's not added because it slightly violates the ideology ofref_t
as per current design, where it wraps rather than just designates the referred variable.For problems that might arise during value-or-reference resolution dilemmas, there's also added
operator+
, which returns a copy of the value held by the designated reference. This follows the general understanding ofoperator+
where it simply returns the value that follows this operator.