Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String view literals in header file

I've got a class that has a bunch of constant strings, in the form of:

using namespace std::string_view_literals;
class T {
  static const constexpr std::string_view something1 = "Alice"sv;
  static const constexpr std::string_view something2 = "Bob"sv;
  static const constexpr std::string_view something3 = "Charlie"sv;

  ...
};

I'm currently using the string_view_literals namespace, but this isn't good practice in a header file, and generates warnings:

Using namespace directive in global context in header [-Wheader-hygiene] (clang)

literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix] (gcc7)

I'd like to look at other options.

  1. Ignore the warnings

  2. Directly import the one literal I'm using, instead of the whole namespace

    using std::string_view_literals::operator""sv

  3. Since this is a constexpr constant, perhaps I should just call the constructor directly, knowing it has no runtime memory or CPU overhead:

    static const constexpr something1 = std::string_view("Alice");

  4. Something else?

like image 321
leecbaker Avatar asked Jun 27 '18 16:06

leecbaker


People also ask

Where is string literal stored C++?

The characters of a literal string are stored in order at contiguous memory locations. An escape sequence (such as \\ or \") within a string literal counts as a single character. A null character (represented by the \0 escape sequence) is automatically appended to, and marks the end of, each string literal.

Where are literal strings stored?

A String Literal, also known as a string constant or constant string, is a string of characters enclosed in double quotes, such as "To err is human - To really foul things up requires a computer." String literals are stored in C as an array of chars, terminted by a null byte.

What is a string literal in C++?

String literals. A string literal represents a sequence of characters that together form a null-terminated string. The characters must be enclosed between double quotation marks.

Is string included in Iostream?

iostream includes string but including string explicitly is a good practice.


1 Answers

This is pretty short and doesn't pollute anything:

class T {
    using sv = std::string_view;
    static constexpr auto something1 = sv("Alice");
    static constexpr auto something2 = sv("Bob");
    static constexpr auto something3 = sv("Charlie");
};

If you really want to use the literal, you could wrap your class in another not-intended-to-be-named namespace and then bring it back into the outer namespace:

namespace _private {
    using namespace std::string_view_literals;

    class T {
        static constexpr auto something1 = "Alice"sv;
        static constexpr auto something2 = "Bob"sv;
        static constexpr auto something3 = "Charlie"sv;
    };
}

using _private::T;

Note that writing static constexpr const is redundant. constexpr variables are implicitly const.

like image 173
Barry Avatar answered Sep 22 '22 18:09

Barry