21

I have some experience in programming in C but I would not dare to call myself proficient. Recently, I encountered the following macro:

#define CONST(x) (x)

I find it typically used in expressions like for instance:

double x, y;
x = CONST(2.0)*y;

Completely baffled by the point of this macro, I extensively researched the advantages/disadvantages and properties of macros but still I can not figure out what the use of this particular macro would be. Am I missing something?

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
Pankrates
  • 3,074
  • 1
  • 22
  • 28
  • 2
    This macro achieves nothing, other than perhaps as a visual reminder that something is a constant... – Oliver Charlesworth Jan 31 '13 at 15:53
  • 2
    The only reason I can think of to do this would be to allow an external tool to more easily find specific constants in the program. It's used for marking text to be translated sometimes although I'm not sure how it would help here – jcoder Jan 31 '13 at 15:56
  • 2
    And it makes it searchable – Minthos Jan 31 '13 at 15:56

7 Answers7

11

As presented in the question, you are right that the macro does nothing.

This looks like some artificial structure imposed by whoever wrote that code, maybe to make it abundantly clear where the constants are, and be able to search for them? I could see the advantage in having searchable constants, but this is not the best way to achieve that goal.

It's also possible that this was part of some other macro scheme that either never got implemented or was only partially removed.

WildCrustacean
  • 5,896
  • 2
  • 31
  • 42
6

Some (old) C compilers do not support the const keyword and this macro is most probably a reminiscence of a more elaborate sequence of macros that handled different compilers. Used like in x = CONST(2.0)*y; though makes no sense.

You can check this section from the Autoconf documentation for more details.

EDIT: Another purpose of this macro might be custom preprocessing (for extracting and/or replacing certain constants for example), like Qt Framework's Meta Object Compiler does.

npclaudiu
  • 2,401
  • 1
  • 18
  • 19
2

There is absolutely no benefit of that macro and whoever wrote it must be confused. The code is completely equivalent to x = 2.0*y;.

Lundin
  • 195,001
  • 40
  • 254
  • 396
1

Well this kind of macro could actually be usefull when there is a need to workaround the macro expansion.
A typical example of such need is the stringification macro. Refer to the following question for an example : C Preprocessor, Stringify the result of a macro

Now in your specific case, I don't see the benefit appart from extreme documention or code parsing purposes.

Community
  • 1
  • 1
greydet
  • 5,509
  • 3
  • 31
  • 51
  • Good point about the stringification macro which I also encountered in my research in an even earlier SO post http://stackoverflow.com/questions/1597007/creating-c-macro-with-and-line-token-concatenation-with-positioning-macr However in the code I found , it is not used in that manner – Pankrates Jan 31 '13 at 16:12
0

Another use could be to reserve those values as future function invocations, something like this:

 /* #define CONST(x) (x) */
 #define CONST(x) some_function(x)

 // ...

 double x, y;
 x = CONST(2.0)*y; // x = some_function(2.0)*y;
higuaro
  • 15,730
  • 4
  • 36
  • 43
0

Another good thing about this macro would be something like this

result=CONST(number+number)*2;

or something related to comparisons

result=CONST(number>0)*2;

If there is some problem with this macro, it is probably the name. This "CONST" thing isn't related with constants but with some other thing. It would be nice to look for the rest of the code to know why the author called it CONST.

Alien
  • 52
  • 3
0

This macro does have the effect of wrapping parenthesis around x during the macro expansion.

I'm guessing someone is trying to allow for something along the lines of

CONST(3+2)*y

which, without the parens, would become

3+2*y

but with the parens becomes

(3+2)*y

I seem to recall that we had the need for something like this in a previous development lifetime.

GaTechThomas
  • 5,421
  • 5
  • 43
  • 69