c++ - 有人可以解释这些函数返回类型的区别吗

标签 c++ c++11

我不明白以下返回类型的区别以及其中一些甚至如何编译?

const bool && const Method1(){
    .....
}

这个的返回是什么?常量(rval)引用常量值?

const bool const Method1(){
    .....
}

Constant bool 常量(我不明白如果我返回一个值,这里怎么会有两个常量)

const bool & const Method1(){
    .....
}

也不知道,常量引用常量值?

哪个最适合用于只读返回值?

最佳答案

  1. const bool const Method1(); 不编译。

  2. const bool&& Method1(); 它编译。关于它的实用性 here一些引用资料。

  3. const bool & const Method1(); 不编译。

引用文献:here , 和 here .


更新

Visual Studio 它给了我

main.cpp(5): warning C4114: same type qualifier used more than once
main.cpp(6): warning C4227: anachronism used: qualifiers on reference are ignored

这只是说 Visual Studio 会忽略该限定符(形式 (1)(3))。

关于c++ - 有人可以解释这些函数返回类型的区别吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39562695/

相关文章:

c++ - 避免在标题中包含计时

c++ - 私有(private)成员的常量访问器之间的比较

c++ - cv::Mat 的序列化给出了奇怪的结果

c++ - 多线程和临界区使用 - C++

c++ - 编译器是否优化了未使用的参数?

c++ - 我什么时候应该使用模板化参数而不是构造参数?

c++ - "Undefined class"作为 is_base_of 的参数

c++ - 具有自定义散列/相等函数的 unordered_map - 函数不会被调用

c++ - 使用 GAlib 模板实例化的 Cygwin gcc 编译器问题

c++ - #ifdef _DEBUG 在主函数中