Reputation: 317
I have my own implementation of a f_sin(x) function,(analogous implementation to sin(x) in ) that I want to use when compiled with a macro named MYMATH. If the MYMATH is not defined I want to use the function sin(x) from the math.h
Any leads on how to go about?
Note I cannot change anything in the function definitions of f_sin(x) or sin(x).
Upvotes: 1
Views: 338
Reputation: 815
You can try using a macro for each function and then just define it depending on your macro MYMATH. Also if you prefer avoiding this kind of macros you can use a generic lambda as a wrapper.
MyMath.hpp
1.- With Macros for each function
#ifdef MYMATH
#define imp_sin(x) f_sin(x)
#else
#include <cmath>
#define imp_sin(x) std::sin(x)
#endif
2. With generic lambda (C++ 14)
#define glambda(x) [](auto y){ return x(y); }
#ifdef MYMATH
auto imp_sin = glambda(f_sin);
#else
#include <cmath>
auto imp_sin = glambda(std::sin);
#endif
#undef glambda //Or not if you want to keep this helper
Usage main.cpp
#include "MyMath.hpp"
int main(int, char**) {
imp_sin(3.4f);
return 0;
}
Upvotes: 1
Reputation: 122460
You can do it like this:
double sin_wrapper(double x) {
#ifdef MYMATH
return f_sin(x);
#else
return std::sin(x);
#endif
}
and then replace all calls to sin
with calls to this wrapper.
Upvotes: 3