std::is_rvalue_reference 在模板函数中使用右值引用参数

std::is_rvalue_reference inside template function with rvalue reference parameter

本文关键字:引用 参数 rvalue is reference std 函数      更新时间:2023-10-16
#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&&

>V不是右值引用 - decltype(element)是(如果element是右值)。 V只是一般类型的element。具体说来:

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