成员静态函数内的 C++ 静态变量

Bub*_*Boy 2 c++ static boost

一般来说,我想知道成员函数的static局部变量存储在哪里?即,如果static变量仅在static函数内部使用,则该变量是否仅初始化一次?

请参考下面的代码

std::string const CONST1 = "const1";
std::string const CONST2 = "const2";
std::string const CONST3 = "const3";

class Test
{
  public:
    static const std::vector<std::string> GetSomeMap();
}

const std::vector<std::string> Test::GetSomeMap()
{
  static std::vector<std::string> SomeMap = boost::assign::list_of(CONST1)(CONST2)(CONST3);
  
  return SomeMap;
}
Run Code Online (Sandbox Code Playgroud)

通过上面的代码,声明为有优势SomeMapstatic?(我希望它只初始化一次。)

R S*_*ahu 5

如果在静态函数内使用静态变量,该变量是否只初始化一次?

答案是“是”。对于常规(即非)成员函数的变量
来说,它也是“是” 。对于非成员函数的变量 也是“是” 。staticstatic
static