문제

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