I have a variable wrapper that signals changes when the underlying variable is modified through this wrapper, so other objects can listen to changes.
It works fine (i.e. I can assign, add, subtract, call member functions on the underlying object (although these have no way of signalling changes), etc.), with one slight hiccup: if I do
struct some_object_type
{
wrapper<int> some_wrapper;
}
int main()
{
some_object_type some_object;
auto value = some_object.some_wrapper;
}
value
here is correctly deduced as a wrapper<int>
. Is there any way to make this construct behave as if some_object.some_wrapper
is of type int
, so that value
is deduced as an int
variable?
@TartanLlama has the correct solution in his comment. However, what would you like auto
to deduce to if instead you had specified auto const& value = ...
? The implicit conversion won't work in that case.
I've run into this problem before, and the best solution I could come up with was to provide an wrapper<T>::operator()
function:
struct some_object_type
{
wrapper<int> some_wrapper;
}
int main()
{
some_object_type some_object;
auto value1 = some_object.some_wrapper; // deduces to wrapper<int>
auto value2 = some_object.some_wrapper(); //deduces to int
}
Maybe not the solution you had hoped for, but unless you forego automatic type deduction, you're best resort is @TartanLlama's suggestion.