阅读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/