Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using the tenary operator in C with 3 numbers
#11
Well, until we get a short-circuiting ternary operator in QB64-PE. Big Grin  

Here is my attempt, just for fun.

My version brazenly exploits how QB64 uses the C++ compiler. This correctly eliminates the part that doesn't require evaluation.

Cheers!


Save as iif.h
Code: (Select All)
#pragma once

#define qb64_iif(qb64_iif_expression, qb64_iif_true_part, qb64_iif_false_part) ((qb64_iif_expression) ? (qb64_iif_true_part) : (qb64_iif_false_part))

Code: (Select All)
OPTION _EXPLICIT

DECLARE LIBRARY "iif"
    FUNCTION IIf_Byte%% ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _BYTE, BYVAL falsePart AS _BYTE)
    FUNCTION IIf_UByte~%% ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _UNSIGNED _BYTE, BYVAL falsePart AS _UNSIGNED _BYTE)
    FUNCTION IIf_Integer% ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS INTEGER, BYVAL falsePart AS INTEGER)
    FUNCTION IIf_Uinteger~% ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _UNSIGNED INTEGER, BYVAL falsePart AS _UNSIGNED INTEGER)
    FUNCTION IIf_Long& ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS LONG, BYVAL falsePart AS LONG)
    FUNCTION IIf_ULong~& ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _UNSIGNED LONG, BYVAL falsePart AS _UNSIGNED LONG)
    FUNCTION IIf_Integer64&& ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _INTEGER64, BYVAL falsePart AS _INTEGER64)
    FUNCTION IIf_UInteger64~&& ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _UNSIGNED _INTEGER64, BYVAL falsePart AS _UNSIGNED _INTEGER64)
    FUNCTION IIf_Single! ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS SINGLE, BYVAL falsePart AS SINGLE)
    FUNCTION IIf_Double# ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS DOUBLE, BYVAL falsePart AS DOUBLE)
END DECLARE

DIM i AS LONG

PRINT IIf_Long(i > 10, foo, bar) ' foo should not evaluate

i = 100

PRINT IIf_Long(i > 10, foo, bar) ' bar should not evaluate

END

FUNCTION foo&
    PRINT "Foo called!"
    foo = 512
END FUNCTION

FUNCTION bar&
    PRINT "Bar called!"
    bar = 128
END FUNCTION


[Image: Screenshot-2024-09-12-184743.png]
Reply


Messages In This Thread
RE: Using the tenary operator in C with 3 numbers - by a740g - 09-12-2024, 01:20 PM



Users browsing this thread: 2 Guest(s)