c++ - "return {...}; "在 C++11 中如何工作?

标签 c++ c++11

我正在尝试实现一个字符串。我想将字符串转换为小写,即所谓的 boost::to_lower_copy(m_s)。事实上,m_sstd::string 类型。我的问题是 return {boost::to_lower_copy(m_s)}; 如何在函数 to_lower_copy() 中工作。它怎么会返回一个 String 类型呢? 非常感谢。

class String {
    std::string m_s;
public:
    String(const std::string s) : m_s(s) { }
    String to_lower_copy() const {
        return {boost::to_lower_copy(m_s)};
    }
};

最佳答案

attr(optional) return braced-init-list ; 是 C++11 中引入的替代返回语句语法。它复制列表初始化函数的返回值。编译器知道函数返回的类型,因为它是声明的一部分。

关于c++ - "return {...}; "在 C++11 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45173680/

相关文章:

c++ - 将大整数作为 vector 中的值传递时出现段错误

c++ - 可变参数模板。一些问题

c++ - 从函数返回的左值引用实际上是右值(从调用者的角度来看)吗?

c++ - 对`i2c_smbus_read_word_data(int, unsigned char) 的 undefined reference

c++ - 加载 OBJ 文件,如何使用法线 (#vertices < #normals)

c++ - 为什么用 C 复制文件比 C++ 快得多?

c++ - 从网络摄像头迁移到 Raspicam 时 OpenCV 的颜色问题

c++ - 如何使用 bcc32 v6.31 + Borland XE 构建 boost 库?

c++ - 如何知道或测试给定类型是否要 move

c++ - 计算机图形学中的最大凸面修补