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

后端 未结 2 1600
长发绾君心
长发绾君心 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:33

    Yes, constexpr on an object declaration means that the object is const. See [dcl.constexpr]/9. And yes, that means that kSomeString in your example has internal linkage.

    The species of ODR violation we are talking about here is not the definition of kSomeString itself, but other definitions that attempt to use it. And there's a problem precisely because of the internal linkage. Consider:

    void f(const std::string_view &);
    
    inline void g() { 
        f(foo::kSomeString); 
    }
    

    This is an ODR violation if included in multiple translation units, essentially because the definition of g in each translation unit references a different object.

提交回复
热议问题