This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Feature request: Attribute to delay evaluation of function argument


Hi All

This is a feature request. To explain, lets say I want to create a
TriBool type, like so:

enum TriBool { False, True, Unknown };

I now want to implement operator&&. I want operator&& to short
circuit, i.e. if the first argument is False the second argument
shouldn't be evaluated.

So I'll make operator&& take a function object for it's second
argument, like so:

TriBool operator&&
(
  TriBool x,
  const std::function<TriBool()>& y
)
{
  if (x == False) { return False; }
  else
  {
    TriBool y_ = y();
    if (x == True) { return y_; }
    else if (y_ == False) { return False; }
    else { return Unknown; }
  }
}

This way if I have:

#define DELAY(x) [&]{ return x; }

TriBool f();
TriBool g();

I can do:

f() && DELAY(g())

and hence have short circuit evaluation.

However, what I'd like to have is just "f() && g()". It would be good
to be able to give the second argument an attribute which basically
wraps any argument passed to it with "DELAY()". Is this possible, or
has it already been done?

Regards,

Clinton


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]