Blo*_*aRd 3 c++ templates const const-cast c++11
删除使用模板化函数的常量我有一点问题.
#include <iostream>
using namespace std;
template< typename T>
void fct(T& param)
{
const_cast<T>(param) = 40;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x = 30;
const int cx = x;
const int& rx = x;
fct(cx);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到:
错误C2440:'const_cast':无法从'int'转换为'int'
我怎么能用const_cast到我的功能.
你可以尝试做这样的事情:
template< typename T>
void fct(const T& param)
{
const_cast<T&>(param) = 40;
}
Run Code Online (Sandbox Code Playgroud)
模板类型T必须是引用,否则您的演员表没有任何意义
| 归档时间: |
|
| 查看次数: |
145 次 |
| 最近记录: |