The task is as follows:

Give examples of the abandonment of object macros and macro functions in favor of language constructs that are not related to the preprocessing stage.

I can’t imagine what the answer might be.

  • #define sqr(x) x*x is bad. template <class T> inline auto sqr(T x) -> decltype(x*x) { return x*x; } template <class T> inline auto sqr(T x) -> decltype(x*x) { return x*x; } - good. Why? sqr(i++); . And in the template also type checking can be added. - int3
  • The example is clear, thank you! - Frostah
  • 2

1 answer 1

Macro functions

 #define summ(x, y) (x)+(y) 

Should be replaced by embedded template functions.

 template<class T> inline T summ(const T &x, const T &y){ return x + y; } 

Macro constants

 #define c 299792458.0 

Should be replaced by constants

 const double c = 299792458.0;