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]

Re: Feature request: Attribute to delay evaluation of function argument


On Sun, Nov 4, 2012 at 12:33 PM, Oleg Endo <oleg.endo@t-online.de> wrote:
> On Sun, 2012-11-04 at 11:02 +1100, Clinton Mead wrote:
>> 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?
>>
>
> I think this can be done without any additional features or extensions.
> Have you tried 'class TriBool' with an 'explicit operator bool', instead
> of overloading operator && for this purpose?
>
> Cheers,
> Oleg
>

Hi Oleg

Could you explain how you get around the following:

(1) Doesn't the non-overloaded operator&& return 'bool', not
'TriBool'? How can it be made to return 'TriBool'?
(2) How can one prevent losing information by converting to 'bool'?
(3) How can technique be applied more generally to functions not named
'&&' or '||' in such a way that the suggested feature would allow?

Thanks,

Clinton


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