Search code examples
c++operator-overloadingc++17c++20conversion-operator

Can assignment to auto have a type other than the result of the r-value expression?


Consider the following:

auto tmp = a + b;

Where a and b are user defined types which returns a proxy object to delay evaluation (this is required for more complex expressions than shown).

Is there a way that the result in this case be something other the result type of the operator overload?

I am wondering if perhaps an implicit conversion operator with the r/l-value specifier T operator() && might be of use here, but I can't quite think how.

I appreciate this question is vague and lacks details, but I think what I want conceptually very simple.

I can think of a way to do this if I didn't want to do auto tmp = ... but rather some_concrete_type tmp = ... but consuming the proxy in the constructor.


Solution

  • auto is always going to deduce its type from the initializer. It is not going to apply a conversion unless you do so yourself (besides the fact that it removes top level reference and cv qualifications).

    If your initialization expression results in some proxy instead of a concrete type, then that is what auto will be deduced to.