Puppy
Puppy

Reputation: 147054

Influencing automatic type deduction

Let's say that I'm writing a function that returns some sort of proxy object, let's say for lazy evaluation or some other purpose. If I write code like

auto x = func();

then x will be the type of the return value - not the type of the object that I wanted proxied. Is it possible to alter auto or decltype so that using them in this situation will return the actual result that I want returned, rather than the type of the proxy object itself?

Upvotes: 4

Views: 180

Answers (2)

balki
balki

Reputation: 27684

Why not type cast it inside func() to what you want when returning?

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92401

Random thoughts:

You could possibly get at the type of the proxied object by using decltype(*func()), or however the proxied object is accessed. There are no modifiers to auto other than the usual const, &, etc.

If it is a lazy evaluation, you probably don't want the final object type right now, do you?

If the proxy has a cenversion operator to the final object, how could auto know that it should be used? What if there are more than one?

Upvotes: 3

Related Questions