Check if return value is consumed

  c++, c++17

Given a function which returns a heavy-to-construct and heavy-to-destroy object, is there a way to skip object construction and destruction if the function return value is not consumed?

HeavyObject func();
auto res = func(); // heavy object is constructed and returned
func(); // lightweight "null" object is returned and immediately destroyed

Is there a technique which I can use except tag dispatching like void func(NotConsumed);?

Source: Windows Questions C++

LEAVE A COMMENT