Search code examples
c++rvalue-reference

std::is_rvalue_reference inside template function with rvalue reference parameter


#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;
}

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


Solution

  • 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