简体   繁体   English

首先是std :: remove_reference还是std :: remove_cv?

[英]std::remove_reference or std::remove_cv first?

If I want to extract the type of a const reference (like double from const double&), do I have to use : 如果我想提取const引用的类型(比如const double&的double),我是否必须使用:

typename std::remove_cv<typename std::remove_reference<Type>::type>::type

or 要么

typename std::remove_reference<typename std::remove_cv<Type>::type>::type

?

Use remove_reference first. 首先使用remove_reference remove_cv removes top level qualifiers only and in case of references, there isn't any (or is ignored). remove_cv仅删除顶级限定符,如果是引用,则不存在任何(或被忽略)。

An example that shows the difference: 显示差异的示例:

#include <iostream>
#include <type_traits>

template<typename T>
using Remove_cv_ref = std::remove_cv<typename std::remove_reference<T>::type>;

template<typename T>
using Remove_ref_cv = std::remove_reference<typename std::remove_cv<T>::type>;

int main()
{
    std::cout << std::is_same<typename Remove_cv_ref<const int&>::type, int>::value; // 1
    std::cout << std::is_same<typename Remove_ref_cv<const int&>::type, int>::value; // 0
}

Live demo. 现场演示。

typename std::remove_cv<typename std::remove_reference<Type>::type>::type

because first remove_reference<const double&>::type is const double , then remove_cv<const double>::type is double . 因为第一个remove_reference<const double&>::typeconst double ,那么remove_cv<const double>::typedouble

But if you have C++11, have a look at std::decay . 但是如果你有C ++ 11,请看看std::decay

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM