2

I'd like to overload operator&& and operator|| on a custom class, and I'd like for the overloaded operators to obey the short-circuiting behavior of their native counterparts.

I know that there is no mechanism for this in the c++ language, but I'm wondering if perhaps gcc might have some sort of extension that would make this possible. I know that such an extension would cause gcc to violate the official c++ standard, but I see a disclaimer on the gcc documentation that reads, "By default, GCC also provides some additional extensions to the C++ language that on rare occasions conflict with the C++ standard", so it doesn't seem outlandish to ask.

If there is a fundamental reason why such a extension simply could not work, even in theory, that would also be good to know for educational purposes. It feels to me like a short_circuit_inline keyword that can be used as a drop-in replacement for inline would have a well-defined meaning and straightforward implementation, but maybe I am wrong.

For what it's worth, I currently have a macro workaround, AND(x,y,...,z) that does a short-circuited x && y && ... && z for instances of my custom class. My desire for the operators is somewhat just for cosmetic purposes.

dshin
  • 2,354
  • 19
  • 29
  • 3
    How would the operators work? They couldn't take two arguments, since you don't know if the second one should be evaluated or not. As an alternative, why not just provide an explicit conversion to `bool` and use the normal operators? – Alan Stokes Jan 16 '16 at 22:48
  • Are [expression templates](http://cpptruths.blogspot.de/2014/09/short-circuiting-overloaded-and-using.html) an option for you? – melak47 Jan 16 '16 at 22:51
  • @AlanStokes I'd imagine that you define the operator with 2 arguments, decorated with my hypothetical short_circuit_inline keyword. The compiler can then inline-replace all calls of that operator with an if-else piece of code, with a variable corresponding to the second parameter only declared in the else-branch. – dshin Jan 16 '16 at 22:53
  • Also, for the suggestions to convert to bool and to use expression templates - they're not ideal for my context, but it will take some effort to explain why. – dshin Jan 16 '16 at 22:56
  • What does your macro actually do? – Alan Stokes Jan 16 '16 at 22:56
  • My macro `AND(x,y)` returns a `binary_expr<_AND,X,Y>`, which includes 2 members of type X and Y, where X=decltype(x) and Y=decltype(y). The constructor of this object copy-constructs x into the X member, and if necessary, y into the Y member. Failure to short-circuit can lead to runtime errors like division-by-zero. – dshin Jan 16 '16 at 23:03
  • Why can't you write an `operator &&` that does the same thing? – Alan Stokes Jan 16 '16 at 23:07
  • My evaluation is not lazy (there is a third field in my `binary_expr` object which stores the primitive value corresponding to the native evaluation, and that field is initialized in the constructor). So if I do `(x!=0 && (1/x))`, then without short-circuiting, I'll end up trying to construct the `binary_expr<_DIV,int,X>` represented by the `1/x`, which will be a division by zero. My macro on the other hand branches based on the value of `x!=0` and chooses to call a constructor that does not initialize the Y member. – dshin Jan 16 '16 at 23:12
  • @AlanStokes Here's an [example](http://stackoverflow.com/a/25913872/543913) of someone who came up with essentially the same macro solution I did. – dshin Jan 23 '16 at 18:51

1 Answers1

1

The answer is "no". All gcc C++ extensions are documented here: https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Extensions.html. There are no extensions listed there that work in the manner you've described.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148