在关于优化和代码风格的C++问题中,在优化副本的上下文中,有几个答案提到了"SSO" std::string.在这种情况下,SSO意味着什么?
显然不是"单点登录"."共享字符串优化",或许?
我一直想知道,C++中的字符串常量有多长.例如,如果我在函数内部创建了一些const char*str ="something",那么返回str的值是否安全?
我写了一个示例程序,看到这样的返回值仍然存储了该字符串,我感到非常惊讶.这是代码:
#include <iostream>
using namespace std;
const char *func1()
{
const char *c = "I am a string too";
return c;
}
void func2(const char *c = "I'm a default string")
{
cout << c << endl;
}
const int *func3()
{
const int &b = 10;
return &b;
}
int main()
{
const char *c = "I'm a string";
cout << c << endl;
cout << func1() << endl;
func2();
func2("I'm not a default string");
cout …Run Code Online (Sandbox Code Playgroud)