Вопрос

A macro like M_PI is pretty useful, but it defines its value as a double. When using it as a float, you can't write M_PIf, because that obviously changes the name of the macro.

How can I append text to a macro?

Это было полезно?

Решение

You can't (at least not without involving more macros).

You can write float(M_PI), or static_cast<float>(M_PI) if you like verbosity.

Другие советы

You can't. Each macro has an individual definition, and you can't just append an f to it. As an alternative, of course, you could always use a second macro which is the version which ends with an f.

But you shouldn't want to do it, anyway. Don't use macros to define numeric constants. Use constants. They're type safe, readable and efficient. They have everything you need and no drawbacks.

#define CONCATENATE_DETAIL(x, y) x##y
#define CONCATENATE(x, y) CONCATENATE_DETAIL(x, y)

CONCATENATE(M_PI,f)

This is more verbose than float(M_PI) and does not always work.

You define another macro:

#define M_PI_F M_PI##f

Although (float)M_PI will be clearer to the reader.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top