No way to have a boolean expression typed once both evaluated and treated as a literal, is there?

EDIT 2: NVM, got it. After I posted, I realized I was trying to make my own version of assert, so made my first edit to this post. After doing that, I realized maybe assert was a define macro instead of something more built directly into the compiler. Found assert.h, where I found __STRING(string). I have known about __FILE__ __LINE__ and __func__, but didn't know about __STRING. So, in case anyone else wants it, solution is:
1
2
3
4
5
6
7
8
#define THROW_IF(boolExpression, humanErrorMessageIfFalse) if(true == (boolExpression)) throw Exception(humanErrorMessageIfFalse, __STRING(boolExpression))
// most people would leave off the "true ==", but I've made it a habit out of always
// evaluating against something... and I've also made a habit out of putting a literal
// on the left side of the evaluation (when possible) to avoid a hard to find error of using
// a single = in the if causing assignment
// this way, if a single = is used, the compiler gives an error that it can't assign the value
// also, either way, i recommend leaving the parenthesis around boolExpression when being evaluated.
// doing so helps prevent some define macro problems 



As you may have noticed in my other post, I like creating my own little c++ extensions including define macros in a limited (3-4) number of scenarios to reduce the amount of code I need to do for operations that I do often enough to justify (to myself) to do so.

I have a class Exception. Rather than typing:
1
2
3
if(someThing != requiredSomething) {
   throw EXCEPTION("someThing must be requiredSomething", "someThing != requiredSomething");
}


I'd rather be able to use something like:
THROW_IF(something != requiredSomething, "someThing must be requiredSomething");

EDIT: Basically, I'm trying to write my own assert, but don't want to use assert because other things need to be done in my situation than just write out to stderr.

I think I'd get close using a function with a bool argument and two strings, but I'd like to avoid having to type the if expression twice. It's obviously short here, but as you know they can get long, and then I have to update the if expression in two places. And, if I copy/paste the code and change a small part, I have to update there too.

I've been trying to make something like:
#define THROW_IF(boolExpression, humanErrorMessageIfFalse) if(true == (boolExpression)) throw Exception(humanErrorMessageIfFalse, "boolExpression")

But, the compiler define processor is making the second argument to Exception's constructor a literal char* with value "boolExpression", rather than substituting boolExpression inside the quotes. I have no doubt it's following the appropriate c++ standards, I'm just wondering if there isn't a way to accomplish what I'm trying to do... Like something I don't know about... Like using tick marks "`boolExpression`", which doesn't help.

Basically, I think it comes down to if I can write a boolean expression only once that will be evaluated during the program running, and made into a literal at compilation time.
Last edited on
Topic archived. No new replies allowed.