Search code examples
c++stringstring-literalsuser-defined-literalsconst-string

User Defined String Literals compared to Const Strings


Consider the following code with these two structures:

std::string operator"" _str(const char* str, std::size_t len) {
    return std::string( str, len );
}

struct MessageLiterals {
    std::string HELP  = "Press F1 for help"_str;
    std::string ABOUT = "Press F2 for about"_str;
    std::string EXIT  = "Press ESC to exit"_str;
};

struct MessageConst {
    const std::string HELP { "Press F1 for help" };
    const std::string ABOUT { "Press F2 for about" };    
    const std::string EXIT { "Press ESC to exit" };
};

int main() {

    MessageLiterals ml;
    std::cout << "Using Literals:\n";
    std::cout << ml.HELP << std::endl;
    std::cout << ml.ABOUT << std::endl;
    std::cout << ml.EXIT << std::endl;
    std::cout << std::endl;

    MessageConst mc;
    std::cout << "Using Constant Strings:\n";
    std::cout << mc.HELP << std::endl;
    std::cout << mc.ABOUT << std::endl;
    std::cout << mc.EXIT << std::endl;

    std::cout << "\nPress any key and enter to quit." << std::endl;
    char c;
    std::cin >> c;

    return 0;
}

A couple of questions come to mind between the two.

  1. Are these considered equivalent or not although they produce the same result?
    • Equivalent in the means of "memory foot print", "compile-run time efficiency", etc.
  2. What are the pros/cons of each.
  3. Are there any advantages - disadvantages one over the other?

I just came across the concept of user-defined literals and I'm trying to get a better understanding of their functionality and usefulness.

EDIT

Okay a little bit of confusion to those trying to answer. I'm familiar with the use of const. The question(s) seems like it is more than one. But generally what I had in mind is harder to put into words or a form of a question, but the overall concept of the difference between the two that I was trying to get at was: Are there any major differences between using "constant std::strings" over "user-defined string literals"?


Solution

  • std::string HELP  = "Press F1 for help"_str;
    

    is no different from

    std::string HELP  = "Press F1 for help";
    

    std::string can be constructed from a C-style string.


    Are these considered equivalent or not although they produce the same result?

    Unless the compiler can perform some more aggressive optimization thanks to const, they are the same.


    What are the pros/cons of each.

    const prevents accidental mutation of your string constants.


    You don't need to use std::string here - you have compile-time constants that could be constexpr:

    struct MessageConst {
        static constexpr const char* HELP { "Press F1 for help" };
        static constexpr const char* ABOUT { "Press F2 for about" };    
        static constexpr const char* EXIT { "Press ESC to exit" };
    };
    

    The code above guarantees no dynamic allocation and ensures that the constants can be evaluated at compile-time.