[SOLVED] Defining literals and constants based on namespaces. | Sololearn: Learn to code for FREE!
+ 3

[SOLVED] Defining literals and constants based on namespaces.

I want to declare some constants which will represent iota for a class of Complex Numbers. The problem is that the default variable is represented by 'i', but engineers and physicists prefer to use 'j', as 'i' already represents current. So I wanted to declare my constants and related literal operators in such a way that whenever the program uses a particular namespace (let's say: engineer), the variables and literals are declared for j, and if not, the variables and literals are declared for i. This is my attempt : https://code.sololearn.com/cQaLGOq1z5AY/#cpp I wanted to use j for literals only when 'using namespace math::engineer' is used. But the issue is the #define from the engineer namespace is called even if the namespace isn't used. Is there any other way I can achieve what I want? Also, is there a pure C++ way to doing so? (using C++17 features available in GCC 8.2. I don't want to wait until C++20. )

6th Nov 2018, 10:18 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
2 Answers
+ 4
I wouldn't do it with namespace. Instead, I'd require the engineers to #define ENGINEERS, if they wanted the engineer definition prior to including the complex header file. The physicists would just include the header file. You would #ifdef to pick the class definitions provided in the header file.
9th Nov 2018, 3:01 PM
John Wells
John Wells - avatar
0
John Wells Thank You Sir!
9th Nov 2018, 3:42 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar