const std :: string&s = nullptr如何作为可选参数

rea*_*esk 5 c++ reference initializer stdstring nullptr

据我所知,引用不能为空,但是当我运行这样的代码时:

#include <iostream>
#include <string>

void test(int i, const std::string& s = nullptr) {
  std::cout << i << " " << s << std::endl;
}

int main() {
  test(1, "test");
  test(2);
}
Run Code Online (Sandbox Code Playgroud)

可选参数s可以为null,并生成代码。此外,test(2)运行时,程序将引发异常,而不是打印一些随机字符串。

当我更改s为int之类的基本类型时,它无法编译,因此我认为魔术仍然保留在字符串类中,但是如何?

而且,如何检查是否s为null?如果我使用if(s==nullptr)if(s.empty()),它将无法编译。

Sto*_*ica 11

test通过使用构造函数5来初始化其参数std::basic_string<char>

basic_string( const CharT* s,
              const Allocator& alloc = Allocator() );
Run Code Online (Sandbox Code Playgroud)

由于它需要具体化一个临时(std::string)才能绑定到该引用。这是因为引用必须绑定到正确类型的对象,而std::nullptr_t事实并非如此。且所述构造函数对要传递的指针具有非null约束。test没有显式参数的调用会导致未定义的行为。

完全清楚地说,在格式良好的C ++程序中,没有空引用之类的东西。引用必须绑定到有效对象。尝试使用进行初始化nullptr将仅寻求进行转换。

由于a std::string是具有明确定义的“空”状态的对象,因此固定版本可以简单地传入默认的初始化字符串:

void test(int i, const std::string& s = {}); // Empty string by default.
Run Code Online (Sandbox Code Playgroud)

一旦违反合同得到解决,s.empty()应再次给出有意义的结果。