What is the safest way to define aliases in C ++?

Suppose I have a Utility class in a utility.h file:

 class Utility { public: static double longDescriptiveName(double x) { return x + 42; } }; 

And then I found that I was using the longDescriptiveName(...) LOT function. So, as the irresponsible C ++ programmer that I am when I had too much coffee, I create a new utilitymacros.h file and add the following to it:

 #define ldn Utility::longDescriptiveName 

Now I include "utilitymacros.h" in any *.cpp where I use ldn(...) , and my heart is filled with joy over how much more convenient it is to use type 3 letters versus 28.

Question: Is there a safer (more correct) way to do this than with #define ?

I noticed that I need to enable "utilitymacros.h" after including the forwards headers, which I clearly don't like, because it is a sign of collisions (although the Boost errors that I get are not very clear regarding what the collision is).

Clarification 1: Reading the code

If you can say that this affects the readability of the code, I assure you that this is not so, because it is a small set of functions that are used by A LOT. An example that is widely known is stoi for stringToInteger . Another is pdf for probabilityDensityFunction etc. Therefore, if I want to do the following, stoi more readable, in my opinion:

 int x = stoi(a) + stoi(b) + stoi(c) + stoi(d); 

than:

 int x = Utility::stringToInteger(a) + Utility::stringToInteger(b) + Utility::stringToInteger(c) + Utility::stringToInteger(d); 

Or:

 int x = Utility::stringToInteger(a); x += Utility::stringToInteger(b); x += Utility::stringToInteger(c); x += Utility::stringToInteger(d); 

Clarification 2: Macro Editor

I use Emacs as my IDE to select Kinesis keyboards, so you KNOW I use a ton of keyboard macros, custom keyboard shortcuts, and actually change what I see in the editor and what is actually stored in h / cpp file. But nevertheless, I feel that the simplicity and visual readability (as stated above) of using the abbreviations of the function in several separate cases are indeed the result I'm looking for (this, of course, depends on the degree).

+6
source share
5 answers

Instead of a macro, you can write an inline function that redirects the call to the actual function:

 inline double ldn(double x) { return Utility::longDescriptiveName(x); } 

This is definitely safer than a macro.

+12
source

You can use the link to the function:

 double (&ldn)(double) = Utility::longDescriptiveName; 
+6
source

How about tweaking snippit / macro / similar stuff in your text editor? Thus, you only need to enter ldn or something like that, and the code should not be run through the preprocessor, at the risk of finding errors later.

+3
source

I don’t know if this helps, but I think that part of the problem may be to use too generic namespaces (or class names in this case), like Utility .

If instead of Utility::stringToInteger we had

 namespace utility { namespace type_conversion { namespace string { int to_int(const std::string &s); } } } 

Then this function can be used locally as follows:

 void local_function() { using namespace utility::type_conversion::string; int sum = to_int(a) + to_int(b) + to_int(c) + to_int(d); } 

Similarly, if classes / structures and static functions are used (and there may be good reasons for this), we have something like

 strut utility { struct type_conversion { struct string { static int to_int(const std::string &s); }; }; }; 

and the local function will look something like this:

 void local_function() { typedef utility::type_conversion::string str; int sum = str::to_int(a) + str::to_int(b) + str::to_int(c) + str::to_int(d); } 

I understand that I am not saying anything about syntax that you did not already know; rather, it is a reminder that the organization and structure of namespaces and classes themselves play an important role in making the code more readable (and writable).

+2
source

One option is to rename your function and put it in the namespace instead of the class, since it is still static. utility.h becomes

 namespace Utility { // long descriptive comment inline double ldn(double x) { return x + 42; } } 

Then you can put using namespace Utility; into your client code.

I know that there are many style guides saying that short names are bad, but I see no reason to obey a certain style and then get around it.

+2
source

All Articles