Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it fine to customize C++?

Tags:

c++

For my projects, I usually define a lot of aliases for types like unsigned int, char and double as well as std::string and others.

I also aliased and to &&, or to ||, not to !, etc.

  • Is this considered bad practice or okay to do?
like image 378
Le syntax Avatar asked Nov 25 '10 21:11

Le syntax


People also ask

What is the full meaning of customs?

1 : the usual way of doing things : the usual practice. 2 customs plural : duties or taxes paid on imports or exports.

What is custom and examples?

The definition of custom is made or designed specifically for an individual. An example of custom is a wedding gown that the bride designed herself. adjective. 1. The tradition or body of such practices.

What is the part of speech of custom?

As a noun, custom means a longstanding practice of a person (such as a daily habit) or a group (such as a cultural practice). As an adjective, custom describes something made to unique specifications, especially something one of a kind.


5 Answers

Defining types to add context within your code is acceptable, and even encouraged. Screwing around with operators will only encourage the next person that has to maintain your code to bring a gun to your house.

like image 142
Ignacio Vazquez-Abrams Avatar answered Oct 03 '22 17:10

Ignacio Vazquez-Abrams


Well, consider the newcomers who are accustomed to C++. They will have difficulties maintaining your project.

Mind that there are many possibilities for the more valid aliasing. A good example is complicated nested STL containers.

Example:

typedef int ClientID;
typedef double Price;
typedef map<ClientID, map<Date, Price> > ClientPurchases;

Now, instead of

map<int, map<Date, double> >::iterator it = clientPurchases.find(clientId);

you can write

ClientPurchases::iterator it = clientPurchases.find(clientId);

which seems to be more clear and readable.

like image 24
Vlad Avatar answered Oct 03 '22 17:10

Vlad


If you're only using it for pointlessly renaming language features (as opposed to the example @Vlad gives), then it's the Wrong Thing.

It definitely makes the code less readable - anyone proficient in C++ will see (x ? y : z) and know that it's a ternary conditional operator. Although ORLY x YARLY y NOWAI z KTHX could be the same thing, it will confuse the viewer: "is this YARLY NOWAI the exact same thing as ? :, renamed for the author's convenience, or does it have subtle differences?" If these "aliases" are the same thing as the standard language elements, they will only slow down the next person to maintain your code.

TLDR: Reading code, any code, is hard enough without having to look up your private alternate syntax all the time.

like image 38
Piskvor left the building Avatar answered Oct 03 '22 17:10

Piskvor left the building


That’s horrible. Don’t do it, please. Write idiomatic C++, not some macro-riddled monstrosity. In general, it’s extremely bad practice to define such macros, except in very specific cases (such as the BOOST_FOREACH macro).

That said, and, or and not are actually already valid aliases for &&, || and ! in C++!

It’s just that Visual Studio only knows them if you first include the standard header <ciso646>. Other compilers don’t need this.

Types are something else. Using typedef to create type aliases depending on context makes sense if it augments the expressiveness of the code. Then it’s encouraged. However, even better would be to create own types instead of aliases. For example, I can’t imagine it ever being beneficial to create an alias for std::string – why not just use std::string directly? (An exception are of course generic data structures and algorithms.)

like image 25
Konrad Rudolph Avatar answered Oct 01 '22 17:10

Konrad Rudolph


  • "and", "or", "not" are OK because they're part of the language, but it's probably better to write C++ in a style that other C++ programmers use, and very few people bother using them. Don't alias them yourself: they're reserved names and it's not valid in general to use reserved names even in the preprocessor. If your compiler doesn't provide them in its default mode (i.e. it's not standard-compliant), you could fake them up with #define, but you may be setting yourself up for trouble in future if you change compiler, or change compiler options.

  • typedefs for builtin types might make sense in certain circumstances. For example in C99 (but not in C++03), there are extended integer types such as int32_t, which specifies a 32 bit integer, and on a particular system that might be a typedef for int. They come from stdint.h (<cstdint> in C++0x), and if your C++ compiler doesn't provide that as an extension, you can generally hunt down or write a version of it that will work for your system. If you have some purpose in mind for which you might in future want to use a different integer type (on a different system perhaps), then by all means hide the "real" type behind a name that describes the important properties that are the reason you chose that type for the purpose. If you just think "int" is unnecessarily brief, and it should be "integer", then you're not really helping anyone, even yourself, by trying to tweak the language in such a superficial way. It's an extra indirection for no gain, and in the long run you're better off learning C++, than changing C++ to "make more sense" to you.

  • I can't think of a good reason to use any other name for string, except in a case similar to the extended integer types, where your name will perhaps be a typedef for string on some builds, and wstring on others.

  • If you're not a native English-speaker, and are trying to "translate" C++ to another language, then I sort of see your point but I don't think it's a good idea. Even other speakers of your language will know C++ better than they know the translations you happen to have picked. But I am a native English speaker, so I don't really know how much difference it makes. Given how many C++ programmers there are in the world who don't translate languages, I suspect it's not a huge deal compared with the fact that all the documentation is in English...

like image 37
Steve Jessop Avatar answered Oct 02 '22 17:10

Steve Jessop