返回初始化列表机制 [英] returning initializer list mechanism

查看:127
本文介绍了返回初始化列表机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

涉及什么机制,如果当返回类型时,可以从初始化列表构造,我不指定我返回的类型,如:

What mechanism is involved, if when returning types, that are constructible from initializer lists, I don't specify the type I am returning, as in:

std::array<int, 3> make_array()
{
  return { 1, 2, 3 };
}

而不是

std::array<int, 3> make_array()
{
  return std::array<int, 3>{ 1, 2, 3 };
}

是否有任何性能损失,如果我返回初始化列表,类型?我实际上返回一个数组,转换为 std :: array

Are there any performance penalties involved, if I return the initializer list without specifying a type? Am I actually returning an array, that is converted into a std::array?

推荐答案

不涉及性能损失。返回值的构造等同于

There are no performance penalties involved. The return value is constructed equivalent to

std::array<int, 3> x = { 1, 2, 3 };

甚至没有单个副本或移动 std :: array 涉及的实例。

There is not even a single copy or move of an std::array instance involved.

这篇关于返回初始化列表机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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