开发者

Influencing automatic type deduction

开发者 https://www.devze.com 2023-03-06 17:01 出处:网络
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

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 ty开发者_如何学运维pe 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?


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消