小编Ona*_*glu的帖子

使用decltype和constness的C ++ 11尾随返回成员函数

我正在尝试使用decltype了解C ++ 11中基于尾随返回的新函数声明语法。

在下面的代码,我试图定义一个成员函数返回一个const&允许只读访问i

#include <iostream>
#include <type_traits>

struct X {
    int &i;

    X(int &ii) : i(ii) {}

//    auto acc() const -> std::add_const<decltype((i))>::type { return i; } // fails the constness test
    auto acc() const -> decltype(i) { return i; } // fails the constness test
//    const int &acc() const { return i; } // works as expected   
};

void modify_const(const X &v) {
    v.acc() = 1;
}

int main() {
    int i = 0; …
Run Code Online (Sandbox Code Playgroud)

c++ const decltype c++11 type-deduction

5
推荐指数
1
解决办法
1120
查看次数

标签 统计

c++ ×1

c++11 ×1

const ×1

decltype ×1

type-deduction ×1