2

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"?

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
  • 2
    Just a small comment, you don't need to define _str, you can use the built-in literal s. – OriBS Nov 24 '17 at 11:45
  • 1
    One question per question please. You should try to narrow it down. – Ron Nov 24 '17 at 11:49
  • Your core question is really about different forms of initialisation, not the fact that the initialising expressions happen to be represented as user-defined literals. – underscore_d Nov 24 '17 at 11:54
  • @OriBS I am familiar about the built-in. I was just trying to get a better understanding of the `literals` and compare them in contrast using the difference between a `const string` & `user defined string literal`. – Francis Cugler Nov 24 '17 at 12:04

2 Answers2

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

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • I know that I can use `constexpr`. I was just trying to get a generalization between the main differences in a `const string` vs `user-defined string literal`. – Francis Cugler Nov 24 '17 at 12:07
1

Are there any advantages - disadvantages one over the other?

Readability inside the struct.

I can understand instantly what is happening inside MessageConst, but I need a moment or two to understand MessageLiterals.


Too many questions in one question, where is should have one question.

gsamaras
  • 71,951
  • 46
  • 188
  • 305