C++ standard: do namespace-scoped constexpr variables have internal linkage?

后端 未结 2 1595
长发绾君心
长发绾君心 2020-12-10 11:59

Imagine we have a header foo.h containing the following:

#ifndef FOO_H_
#define FOO_H_

namespace foo {
constexpr std::string_view kSomeString =         


        
2条回答
  •  春和景丽
    2020-12-10 12:43

    Your usage of kSomeString is perfectly valid.

    First things first; your object is const-qualified, as T.C. explained. Further is it of literal type, because it has a constexpr constructor. Therefore if you use it in a function defined in a header file, the following exception in the standard applies (chapter 3.2):

    There can be more than one definition of an inline function with external linkage (7.1.2), non-static function template (14.5.6), (…) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

    • each definition of D shall consist of the same sequence of tokens; and
    • in each definition of D, corresponding names, looked up according to 3.4, shall refer to an entity defined within the definition of D, or shall refer to the same entity, after overload resolution (13.3) and after matching of partial template specialization (14.8.3), except that a name can refer to a const object with internal or no linkage if the object has the same literal type in all definitions of D, and the object is initialized with a constant expression (5.19), and the value (but not the address) of the object is used, and the object has the same value in all definitions of D;
    • (…)

提交回复
热议问题