0

I would like to write a macro in C++ that, based on a comparison, returns either comma and a value, or nothing at all.

#define TEST1(x) \
    x == 1 ? COMMA 2 : NADA 

#define COMMA ,
#define NADA

The idea is that when the argument is 1 the macro will return , 2 and otherwise will return nothing, so that

int foo[5] = { 0 TEST1(1) TEST1(2) };

would compile as:

int foo[5] = { 0 , 1 };

The first error is: missing '}' before constant.

Charles
  • 479
  • 1
  • 3
  • 13
  • 2
    Macros do not return anything. Macros are replaced, as text. – hyde Oct 29 '14 at 15:52
  • Thanks. Please read the sentence as "the macro will be replaced by ", 2" (without the quotes) or nothing at all. – Charles Oct 29 '14 at 15:54
  • Yes, and should be would compile as int foo[5] = { 0 , 2 }; – Charles Oct 29 '14 at 15:55
  • Just use, what you got, TEST3 = same as TEST1 just with ',' exept that `TEST2 = TEST3 concatenated TEST1` and use the Marko string concatenation http://stackoverflow.com/questions/5256313/c-c-macro-string-concatenation – mike Oct 29 '14 at 15:57
  • 2
    macro is a wrong tool for this job. Please explain what you want to achieve. – qrdl Oct 29 '14 at 16:00
  • 2
    possible duplicate of [C preprocessor macro specialisation based on an argument](http://stackoverflow.com/questions/11632219/c-preprocessor-macro-specialisation-based-on-an-argument) – Dettorer Oct 29 '14 at 16:04
  • 2
    I've done my best to remove fluff from the question, however, take a look at [What is the XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) –  Oct 29 '14 at 16:06
  • I see what you are saying. I suspect the problem cannot be solved with a macro. I guess there is no way to condition a comma. There is no way to generate , x in some circumstances and nothing in others. – Charles Oct 29 '14 at 17:01

2 Answers2

2

Your macro doesn't do what you think it does. Macro is simply textual replacement, so it will substitute TEST(abc) for "x == abc ? , 2 : ". It will not evaluate the expression at compile time.

Your code won't compile with this substitution. You can run gcc -E file.c to see what the compiler is doing when preprocessing your code. This is the output:

int main() {
  int foo[5] = {0 (1 == 1) ? , 2 : (2 == 1) ? , 2 : };
  return 0;
}

You probably shouldn't do macros to accomplish this. Tell us why you're trying to this and we might be able to provide you with a more suitable alternative.

Márcio Paiva
  • 983
  • 9
  • 25
  • Okay. I am working on this problem: http://stackoverflow.com/questions/26613420/how-readily-do-unterminated-char-arrays-in-c. Rather than have half a dozen different macros: MACRO_GET_4, MACRO_GET_8, etc. I was hoping I could write one macro that took a "number of characters" argument and had a set of ,str[i] results based on the value of the number of characters argument. – Charles Oct 29 '14 at 16:54
  • Okay. XY Problem. The structs with unterminated fixed-length char's are a fact of life. I did not invent them. I want to define such a struct in my code as very simple test data. What is the best way to initialize an unterminated char array in a stuct definition, without resorting to { 'H', 'e', 'l', 'l', 'o', ...} ? The link above is pretty good, but is it possible to do initialize a variety of array lengths without having to write a separate macro for each length? – Charles Oct 29 '14 at 18:13
1

You shouldn't write such macros yourself, unless for the interlectual pleasure. Better use existing packages such as Boost or P99. In P99 you have a whole bunch of preprocessor conditionals such as P99_IF_EQ that tests two numbers for equality and chooses from two values. For your problem this seems to be something like P99_IF_EQ(X,1)(, 2)().

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • Heck no, I am looking for an answer, not a macro intellectual challenge. Found P99 after wading through a bunch of pistol ads. :-( Looks like you are the author. I will take a look at it. Not sure if they will work with VS 2010 as I don't think I have variadic macro support. – Charles Oct 29 '14 at 18:33
  • I must really be stupid. I am looking at http://p99.gforge.inria.fr/. Where is the link to download the current version? – Charles Oct 29 '14 at 18:42
  • In the directory versions, the latest one is from Aug 15. Ah, and you didn't say that you were looking for code for an obsolete compiler, that one is indeed not conforming. – Jens Gustedt Oct 29 '14 at 19:18
  • "versions" is described as *previous* versions -- that's what confused me. 2010 is hardly ancient history, but okay. FWIW, here is the error I am getting: Error 1 error C1112: compiler limit : '160' too many macro arguments, only '127' allowed p99\p99_generated.h 74 – Charles Oct 29 '14 at 19:37
  • That is probably only a polite way for your compiler saying, sorry I can't deal with this... – Jens Gustedt Oct 29 '14 at 20:15
  • "Go away -- don't bother me with this stuff." NP, thanks for your help. I have some of those do-it-yourself macros completed and working. Take care. – Charles Oct 29 '14 at 22:06
  • 1
    @Charles, that's not at all what I wanted to say. But to my experience the MS compilers are difficult to get in line with that stuff. They still haven't caught up with C99, which is out 15 years now. And as the name indicates, P99 builds on C99, in particular it needs that the preprocessor is conforming. On MS platforms you have plenty of choice for compilers that are conforming. – Jens Gustedt Oct 30 '14 at 07:55