常规字符串字符串文字具有以下定义:
普通字符串文字和UTF-8字符串文字也称为窄字符串文字.窄字符串文字的类型为"n const char数组",其中n是下面定义的字符串大小,并且具有静态存储持续时间(3.7).
我假设因为它具有静态存储持续时间并且它们通常被放置在ROM中,如果有一个悬挂引用它真的没什么大不了的.以下代码发出警告
const char* const & foo()
{
return "Hello";
}
// warning: returning reference to temporary [-Wreturn-local-addr]
Run Code Online (Sandbox Code Playgroud)
但即使没有static关键字,这也没关系
const char* const & foo()
{
const char* const & s = "Hello";
return s;
}
Run Code Online (Sandbox Code Playgroud)
那两者有什么区别?