Earlier quoted context omitted.
> #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.
What are unevaluated operands in C++?
11–20 of 20 posts
Re: What are unevaluated operands in C++?
#12> Substitution failure is not an error (SFINAE) refers to a situation in C++ where an invalid substitution of template parameters is not in itself an error.
https://en.m.wikipedia.org/wiki/Substitution_failure_is_not_...
Re: What are unevaluated operands in C++?
#13If you don’t know what SFINAE is (the article uses the acronym a lot but doesn’t define it): > Substitution failure is not an error (SFINAE) refers to a situation in C++ where an invalid substitution of template parameters is not in itself an error. https://en.m.wikipedia.org/wiki/Substitution_failure_is_not_...
Re: What are unevaluated operands in C++?
#14> Up to C++17, there are four operators the operands of which are unevaluated: typeof, sizeof, decltype, and noexcept.
"typeof" should be "typeid". This confused me a bit as I thought the article was going to talk about a nonstandard predecessor to decltype.
The article was interesting, but its contents didn't really match it's title. It's not about unevaluated operands in general but just their use in SFINAE specifically. For example, sizeof is dismissed as not much use for SFINAE, which is true but it certainly has a lot of other uses! And if you read this article having not seen decltype before, I think you'd get the wrong impression about its range of uses.
Re: What are unevaluated operands in C++?
#15This 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) do {} while (false && ((expr), false))
This doesn't require expr to be convertible to bool.Re: What are unevaluated operands in C++?
#16Small typo near start: > Up to C++17, there are four operators the operands of which are unevaluated: typeof, sizeof, decltype, and noexcept. "typeof" should be "typeid". This confused me a bit as I thought the article was going to talk about a nonstandard predecessor to decltype. The article was interesting, but its contents didn't really match it's title. It's not about unevaluated operands in general but just thei…
Re: What are unevaluated operands in C++?
#17Re: What are unevaluated operands in C++?
#18I'm a c++ programmer but honestly hate the language more and more with every year, because it's so complex. Can somebody explain the first code sample to me?
Re: What are unevaluated operands in C++?
#19This 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)
Note that lambda expressions are not allowed in an unevaluated context. What about this? #define MY_ASSERT(expr) do {} while (false && ((expr), false)) This doesn't require expr to be convertible to bool .
Re: What are unevaluated operands in C++?
#20This 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) ((void)(false && (expr)))
The problem the do-while idiom is that the macro result is not an expression, so it can’t be used in certain cases.