小编Kir*_*lak的帖子

使模板仅适用于基本数据类型

我们如何使模板仅接受基本数据类型。

template <typename T> 
void GetMaxValue( T& x ) 
{ 
//... Finds max Value
} 
Run Code Online (Sandbox Code Playgroud)

在上面的函数中,GetMaxValue我们可以传递任何值而不会出现任何错误。

但是std Function std::numeric_limits<T>::max()已经处理了它。例如:

auto max = std::numeric_limits< std::map<int,int> >::max();
Run Code Online (Sandbox Code Playgroud)

会给出一个错误 error C2440: '<function-style-cast>' : cannot convert from 'int' to 'std::map<_Kty,_Ty>'

c++ templates c++11

2
推荐指数
1
解决办法
65
查看次数

std::move 不适用于 RValue 参考函数

在尝试学习 std::move 和右值引用时,我刚刚遇到以下内容:

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<int> vecNumbers;
    vecNumbers.push_back(10);
    vecNumbers.push_back(20);

    foo(std::move(vecNumbers));

    std::cout<<"After Move \n";
    std::cout<<"size:"<<vecNumbers.size()<<"\n";

    return 0;
}

void foo(  std::vector<int> &&value)
{
    std::cout<<"size in Function:"<<value.size()<<"\n";
}
Run Code Online (Sandbox Code Playgroud)

输出

size in Function:2
After Move
size:2
Run Code Online (Sandbox Code Playgroud)

在调用向量上的移动后,我预计大小为 0 ,但这里它仅作为参考移动。有人可以解释一下这里发生了什么吗?

c++ vector std rvalue-reference c++11

1
推荐指数
1
解决办法
820
查看次数

标签 统计

c++ ×2

c++11 ×2

rvalue-reference ×1

std ×1

templates ×1

vector ×1