我正在尝试实现一个字符串。我想将字符串转换为小写,即所谓的 boost::to_lower_copy(m_s)
。事实上,m_s
是 std::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/