在C++中定义常量C字符串的正确方法?

voi*_*ter 13 c++ const cstring auto c++11

大多数时候我看到常量C字符串定义为:

static char const* MY_CONSTANT = "Hello World";
Run Code Online (Sandbox Code Playgroud)

但是,指针本身不是const.像下面这样做是不是更合适?

static char const* const MY_CONSTANT = "Hello World";
Run Code Online (Sandbox Code Playgroud)

我认为有两个像这样的常量全局变量的目标:

  1. 不允许修改字符串
  2. 不要让变量指向其他任何东西

我只是假设在定义常量字符串时需要这两个目标.

另一个有趣的事情是我被允许这样做:

int main()
{
    auto MY_CONSTANT = "";
    MY_CONSTANT = "Another String";
}
Run Code Online (Sandbox Code Playgroud)

这告诉我,auto推断字符串char const*是否char const* const.

所以我有两个主要问题:

  1. 定义常量c风格字符串的最合适方法是什么(我认为常量指针指向某事,是更普遍的问题?).为什么选择其中一个?
  2. 关于我的例子auto,它选择的char const*原因是有道理的(因为它是恒定的数据数组,而不是指针本身).我可以auto推断或者我char const* const可以更改代码以使其产生这样的类型吗?

Sha*_*our 9

好吧,如果它真的是一个常数,那么constexpr将是C++ 11的方法:

constexpr char const* MY_CONSTANT = "Hello World";
Run Code Online (Sandbox Code Playgroud)

第一个例子:

static char const* MY_CONSTANT = "Hello World";
Run Code Online (Sandbox Code Playgroud)

只是说我有一个指向具有静态存储持续时间的char const的指针,如果它在函数之外会使它成为全局.

如果我们要求指针也是const,那么我们需要你介绍的第二种形式.这一切都取决于指针是否确实应该是const.在大多数情况下,你看到没有顶级const的代码的原因是因为他们只是忘了把它放入,因为它们并不意味着指针也是const.

其中静态使得例如一个区别,如果你是要一个常量成员是每个实例或每个类:

class A
{
        char const* const const_per_instance = "Other Const String";
    public:
        constexpr static char const* const const_per_class = "Hello World" ;
};
Run Code Online (Sandbox Code Playgroud)

如果我们要求const是每个类,那么我们需要使用静态,否则不需要.如果您不允许使用constexpr,示例会略有变化:

class A
{
        char const* const const_per_instance = "Other Const String";
    public:
        static char const* const const_per_class  ;
};

char const* const A::const_per_class = "Hello World" ;
Run Code Online (Sandbox Code Playgroud)

但实质是相同的只是语法不同.

对于你的第二个问题,因为Gotw#92表示自动降低顶级const,给出的一个例子如下:

const int   ci  = val;  
auto        g   = ci;
Run Code Online (Sandbox Code Playgroud)

它说:

g的类型是int.

请记住,仅仅因为ci是const(只读)与我们是否希望g为const无关.这是一个单独的变量.如果我们想要g为const,我们就像上面的情况c那样说过const auto

正在提到的例子如下:

int         val = 0;  
//..
const auto  c   = val;
Run Code Online (Sandbox Code Playgroud)