模板函数内的 std::is_rvalue_reference 带有右值引用参数 [英] std::is_rvalue_reference inside template function with rvalue reference parameter

查看:41
本文介绍了模板函数内的 std::is_rvalue_reference 带有右值引用参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
#include<vector>

struct Empty {};

template <typename V>
void add(V&& element)
{
    static_assert( std::is_rvalue_reference<V>::value, "V is not a rvalue reference");
}

int main(int argc, char *argv[])
{
    add(Empty());   

    std::cin.ignore();
    return 0;
}

我不明白为什么 static_assert 在这里失败,V 在这里不等于 V&& ?

I don't get why static_assert fail here, V isn't equal to V&& here ?

推荐答案

V is not the rvalue-reference - decltype(element) is (if element 是一个右值).V 只是element 的一般类型.具体:

V is not the rvalue-reference - decltype(element) is (if element is an rvalue). V is simply the general type of element. Specifically:

typename std::remove_reference<decltype(element)>::type; // V

这篇关于模板函数内的 std::is_rvalue_reference 带有右值引用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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