Live data from Hacker News

What are unevaluated operands in C++?

humanreadablemag.com

1–10 of 20 posts

Re: What are unevaluated operands in C++?

#5
post #2

Poor man's lazy evaluation. I'm sure they'll try to shoehorn monads in somehow with C++23

It's not lazy evaluation at all. Lazy evaluation is a runtime thing, where you delay evaluating an expression until you actually need to.

This concept is operators that will never evaluate the expressions they operate on.

sizeof is the oldschool obvious one, sizeof(foo()) never runs the function foo. It's just a way of querying the static properties of an expression, asking specific things about its type in a way.

They're not new things in C++. sizeof has been around since the beginning.

Re: What are unevaluated operands in C++?

#7
This seems like a potential solution to the problem of how to #define MY_ASSERT(x) in release mode such that it fails to compile if "x" is not a valid expression.

Previous I have used this idiom:

#define MY_ASSERT(expr) do {} while (false && (expr))

Another possible alternative might be:

#define MY_ASSERT(expr) sizeof(expr)

Re: What are unevaluated operands in C++?

#8
post #7

This seems like a potential solution to the problem of how to #define MY_ASSERT(x) in release mode such that it fails to compile if "x" is not a valid expression. Previous I have used this idiom: #define MY_ASSERT(expr) do {} while (false && (expr)) Another possible alternative might be: #define MY_ASSERT(expr) sizeof(expr)

> #define MY_ASSERT(expr) sizeof(expr)

sizeof accepts a type or an expression as its input, so it would compile successfully if expr were a type but not a valid expression (say, int).

Re: What are unevaluated operands in C++?

#9
post #8
post #7

This seems like a potential solution to the problem of how to #define MY_ASSERT(x) in release mode such that it fails to compile if "x" is not a valid expression. Previous I have used this idiom: #define MY_ASSERT(expr) do {} while (false && (expr)) Another possible alternative might be: #define MY_ASSERT(expr) sizeof(expr)

> #define MY_ASSERT(expr) sizeof(expr) sizeof accepts a type or an expression as its input, so it would compile successfully if expr were a type but not a valid expression (say, int).

You could do sizeof((expr)?1:2) instead, which would also force expr to be contextually convertible to a bool.
Post reply on HN