"return {}"是什么意思?语句在C ++ 11中意味着什么? [英] What does "return {}" statement mean in C++11?

查看:709
本文介绍了"return {}"是什么意思?语句在C ++ 11中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明是什么

return {};

C ++ 11中的

指示以及何时使用它代替(例如)

in C++11 indicate, and when to use it instead of (say)

return NULL;

return nullptr;

推荐答案

return {};表示返回具有空

return {}; indicates "return an object of the function's return type initialized with an empty list-initializer". The exact behaviour depends on the returned object's type.

来自 cppreference.com (因为OP被标记为C ++ 11,因此我排除了C ++ 14和C ++ 17中的规则;有关更多详细信息,请参阅链接):

From cppreference.com (because the OP is tagged C++11, I excluded the rules in C++14 and C++17; refer to the link for further details):

  • 如果braced-init-list为空,并且T是具有默认构造函数的类类型,则将执行值初始化.
  • 否则,如果T为聚合类型,则执行聚合初始化.
  • 否则,如果T是std :: initializer_list的特殊化,则根据上下文,从braced-init-list直接初始化或复制T对象.
  • 否则,将分两个阶段考虑T的构造函数:

  • If the braced-init-list is empty and T is a class type with a default constructor, value-initialization is performed.
  • Otherwise, if T is an aggregate type, aggregate initialization is performed.
  • Otherwise, if T is a specialization of std::initializer_list, the T object is direct-initialized or copy-initialized, depending on context, from the braced-init-list.
  • Otherwise, the constructors of T are considered, in two phases:

  • 检查所有将std :: initializer_list作为唯一参数,或将第一个参数(如果其余参数具有默认值)作为第一个参数的构造函数,并通过重载解析将其与std :: initializer_list类型的单个参数匹配
  • 如果上一阶段未产生匹配,则T的所有构造函数都将针对由braced-init-list元素组成的参数集参与重载解析,并限制仅允许非缩小的转换.如果此阶段生成一个显式构造函数作为复制列表初始化的最佳匹配,则编译将失败(请注意,在简单的复制初始化中,根本不考虑显式构造函数).

否则(如果T不是类类型),如果braced-init-list仅具有一个元素,并且T不是引用类型或与之类型兼容的引用类型元素,T是直接初始化的(在direct-list初始化中)或副本初始化的(在copy-list初始化中),但不允许缩小转换.

Otherwise (if T is not a class type), if the braced-init-list has only one element and either T isn't a reference type or is a reference type that is compatible with the type of the element, T is direct-initialized (in direct-list-initialization) or copy-initialized (in copy-list-initialization), except that narrowing conversions are not allowed.

在C ++ 11之前,对于返回std::string的函数,您应该编写:

Before C++11, for a function returning a std::string, you would have written:

std::string get_string() {
    return std::string();
}

使用C ++ 11中的大括号语法,您无需重复该类型:

Using the brace syntax in C++11, you don't need to repeat the type:

std::string get_string() {
    return {}; // an empty string is returned
}

函数返回指针类型时,应使用

return NULLreturn nullptr:

return NULL and return nullptr should be used when the function returns a pointer type:

any_type* get_pointer() {
    return nullptr;
}

但是,NULL自C ++ 11起就被弃用,因为它只是整数值(0)的别名,而nullptr是真正的指针类型:

However, NULL is deprecated since C++11 because it is just an alias to an integer value (0), while nullptr is a real pointer type:

int get_int() {
    return NULL; // will compile, NULL is an integer
}

int get_int() {
    return nullptr; // error: nullptr is not an integer
}

这篇关于"return {}"是什么意思?语句在C ++ 11中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆