Putting a single # before a macro causes it to be changed into a string of its value, instead of its bare value. stringify usage: S(USER) or S(USER_VS) when you need the string form.ĪNSI token pasting is sometimes less than obvious. #define USER_VS USER#_VS // jack_VS or queen_VS #define USER jack // jack or queen, your choice If you really must do compile time sting comparisons, then you need to change to C++11 or newer variants that allow that feature. You can't do compile time string compares in C99, but you can do compile time choosing of strings. So, ultimately, you will have to change the way you accomlish your goal of choosing final string values for USER and USER_VS. #define USER JACK // or QUEEN, your choice some compilers may require ((int)lhs - (int)rhs) See comment by // compares two strings in compile time constant fashionĬonstexpr int c_strcmp( char const* lhs, char const* rhs ) [2021.01.04: CAVEAT: This does not work in any MODERN compiler. If you define a compile time helper function for the comparison. #define CHOICE JACK // or QUEEN, your choice It does allow characters, so if you limit yourself to characters you might use this: #define JACK 'J' The definition of const-expression used in the #if does not allow strings. No amount of MACRO manipulation will change this. So, YMMV on this (or any) answer involving constexpr, depending on the compiler writer's interpretation of the spec.Īs some didn't like my earlier answer because it avoided the whole compile time string compare aspect of the OP by accomplishing the goal with no need for string compares, here is a more detailed answer. That word can makes all the difference in the world. >[ Note: Constant expressions can be evaluated during Section 5.19 Constant expressions that says this though: Time, we only have a non-normative quote from draft C++ standard Is this really a constexpr?Ĭonstant expressions are not guaranteed to be evaluated at compile Quoting from Shafik Yaghmour's answer at: Computing length of a C string at compile time. The below code works in the compiler I tested on, while many commenters used a different compiler. That said, the 2016 code (using characters, not strings) still works in VS2019.īut, as points out, the version involving c_strcmp will NOT work in ANY modern compiler.ĬAVEAT: Not all compilers implement the C++11 specification in the same way. Nowadays, the parens are required! #pragma message ("USER IS " USER) The acceptability of with was only slightly greater than that of to (84 percent to 76 percent), and with might have been even more acceptable had the sentence been about two forged signatures.One thing that has changed since I first posted this in 2014, is the format of #pragma message. The margin of acceptability was slimmer for a sentence about assessing the similarities and differences between two comparable items: The police compared the forged signature with the original. The 2014 Usage Survey presented He compared the runner to a gazelle, where the items are in different categories and the first is likened to the second the Panelists found to more acceptable than with by a large margin (95 percent to 55 percent). It's a subtle distinction, and most writers accept both prepositions for both kinds of comparison, though with a preference that aligns with the traditional rule. With implies "together" or "side by side," and so comparing the Senate version of the bill with the House version means treating them symmetrically, as two examples of the same kind of entity, and noting both the similarities and the differences. To implies "in the direction of" or "toward a target," and so comparing Miriam to a summer's day means treating the summer's day as a standard or paragon and noting that Miriam, though a different kind of entity, is similar in some ways to it. Usage Note: A common rule of usage holds that compare to and compare with are not interchangeable.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |