c++ - 函数定义后 "const -> std::string const&"的含义?

标签 c++ c++14

阅读C++ Primer, 5th Edition中一​​个练习的答案,我发现了这段代码:

#ifndef CP5_ex7_04_h
#define CP5_ex7_04_h
#include <string>

class Person {
std::string name;
std::string address;
public:

auto get_name() const -> std::string const& { return name; }
auto get_addr() const -> std::string const& { return address; }
};

#endif

是什么

const -> std::string const& 

在这种情况下是什么意思?

最佳答案

auto get_name() const -> std::string const& { return name; } 是等价的 尾随返回类型 符号

std::string const& get_name() const { return name; }

请注意,在您可以使用一种语法声明一个函数并使用另一种语法定义它的意义上,等价是精确

(自 C++11 以来,这一直是 C++ 标准的一部分)。

关于c++ - 函数定义后 "const -> std::string const&"的含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39104690/

相关文章:

c++ - 从父命名空间重载类型

c++ - 为什么构造函数需要范围解析来在主体之外定义它?

c++ - 如何同时使用变量和 move 变量?

c++ - std::experimental::optional inside constexpr 函数

c++ - 在编译时区分别名和真实类型?

c++ - 使用临时数组作为左值

c++ - 带链表的嵌套数据结构

c++ - 在 Linux 中使用 gdb 调试 Firebreath

c++ - 用 g++ 编译 c++14 代码

c++ - 为什么 void_t<> 检测习语不适用于 gcc-4.9?