std::is_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;
}
我不明白为什么static_assert在这里失败,V
不等于这里的V&&
?
>V
不是右值引用 - decltype(element)
是(如果element
是右值)。 V
只是一般类型的element
。具体说来:
typename std::remove_reference<decltype(element)>::type; // V
相关文章:
- 将const引用参数初始化为默认参数会导致悬空引用吗
- 具有常量引用参数的函数模板专用化
- C++:常量引用参数
- 字符串引用参数的效率C++
- 通过非常量引用参数修改常量引用参数
- 如何将指针变量作为引用参数传递?
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- 移动类的成员作为常量引用参数传递
- C++带有适用于左值和右值的引用参数的函数
- constexpr 函数的常量引用参数:gcc/msvc vs clang/icc
- 如何使用类型特征将函数的通用引用参数限制为 r 值引用?
- 委托构造函数和引用参数
- 对 const 引用参数使用默认值会导致崩溃
- 为什么我们不允许将纯引用参数传递给 std::thread,但允许传递原始指针?
- 为什么我需要将默认引用参数定义为 const 以便为其分配一个左值?
- 将非左值作为常量引用参数传递.临时是在本地作用域还是在调用方作用域中创建的?
- 如何强制函数仅接受左值引用参数
- 模板引用参数推断失败C++
- 非类型引用参数可以在运行时修改,这是否意味着模板可以在运行时实例化?
- 将unique_ptr作为引用参数或常量传递unique_ptr引用