Tip of the Week #168: inline Variables

Originally posted as TotW #168 on September 12, 2019

By James Dennett

Updated 2020-04-06

Quicklink: abseil.io/tips/168

Here’s one safe way to define a string constant in a header file with C++17’s inline variables.:

inline constexpr absl::string_view kHelloWorld = "Hello World.";

Safety of initialization and destruction is ensured by the use of constexpr, and using inline here ensures that there is only one copy of kHelloWorld in the program.

Using the keyword inline here may seem strange at first, particularly if you are used to thinking of inline as being primarily an optimization hint. The use of inline for functions in headers is a close analogy; compare the variable definition above to something like

inline constexpr absl::string_view HelloWorld() {
  return "Hello World.";
}

but with the advantage that the string is guaranteed to be at the same memory address every time.

Almost every global variable defined in a header file should be marked as inline – and should generally be constexpr too. If they are not marked as inline then there will be a separate instance of the variable for each .cc file that includes the header, which can lead to subtle violations of the ODR (one definition rule).

Outside of header files there is no need to mark variables as inline.

Note: A static constexpr data member of a class is implicitly inline from C++17. This special case does not change the semantics of existing code, but means that it is now unnecessary to provide a separate definition for the member in a source file. This applies only to static constexpr data members, not to other constexpr variables, and not to data members that are merely static const.

References:


Subscribe to the Abseil Blog