Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best "type renaming" method?

If I wanted to rename the std::string type to a simpler and more naturally looking string, which of these two methods should I use (based on performance and what is usually the standard)

Should I rename it as a preprocessed directive

#define string std::string

Or do it with a type definition

typedef std::string string;

What's the most performant method? What is also more familiar and recognized to the community?

like image 378
user6245072 Avatar asked Oct 14 '25 09:10

user6245072


1 Answers

Just

using std::string;

If you want a slightly different name, e.g.

using String = std::string;

Avoid macros, they don't respect scopes and are generally Evil™.

For example, the proposed macro

#define string std::string

… yields formal Undefined Behavior if you include any standard library header, because it defines a name used by the standard library.

C++11 §17.6.4.2.2/1 [macro.names]:

A translation unit that includes a standard library header shall not #define or #undef names declared in any standard library header.

like image 130
Cheers and hth. - Alf Avatar answered Oct 16 '25 21:10

Cheers and hth. - Alf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!