Search code examples
c++typedefuser-defined-typespreprocessor-directive

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?


Solution

  • 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.