引用const相当于std :: is_const是什么? [英] What's the equivalent of std::is_const for references to const?

查看:126
本文介绍了引用const相当于std :: is_const是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑代码:

int const  x = 50;
int const& y = x;
cout << std::is_const<decltype(x)>::value << endl; // 1
cout << std::is_const<decltype(y)>::value << endl; // 0

这很有意义,因为 y 不是 const 引用,而是对 const 的引用。

This makes sense, because y is not a const reference, it is a reference to a const.

是否有 foo 使得 std :: foo< decltype(y)> ; :: value 是1?如果没有,那么定义我自己的将是什么样?

Is there a foo such that std::foo<decltype(y)>::value is 1? If not, what would it look like to define my own?

推荐答案

使用删除参考

#include <string>
#include <iostream>
#include <type_traits>
using namespace std;

int main()
{
    int const  x = 50;
    int const& y = x;
    cout << std::is_const<std::remove_reference<decltype(x)>::type>::value << endl; // 1
    cout << std::is_const<std::remove_reference<decltype(y)>::type>::value << endl; // 1

    return 0;
}

在coliru上查看

这篇关于引用const相当于std :: is_const是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆