作者su27 (su27)
看板C_and_CPP
标题[问题] template + vector
时间Tue Mar 29 18:12:44 2022
开发平台(Platform): (Ex: Win10, Linux, ...)
win10
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
qt
问题(Question):
dll的函数
void fun_1(vector<uchar> &val)
void fun_2(vector<char> &val)
我在qt写一个函数
功能是
填写不同的dataType
调用来自DLL不同的函数
template <class type>
static void Select_fun(vector<type> vtp)
{
if (std::is_same<type, uchar>::value)
{
vector<uchar> &temp=vtp; //<<这边报错
fun_1(temp);
}
else if (std::is_same<type, char>::value)
{
fun_2(vtp); //<<这边报错
}
}
错误讯息
//------------------------------------
error: invalid initialization of reference of type
'std::vector<unsigned char>&' from expression of type
'std::vector<int>'
vector<uchar> &temp=vtp;
^~~~
//------------------------------------
请问我该如何解决 谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 125.228.53.160 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1648548768.A.2A6.html
※ 编辑: su27 (125.228.53.160 台湾), 03/29/2022 18:16:52
1F:→ sarafciel: 模板不会帮你绕掉type checking,请把偏特化拿出来用 03/29 18:19
2F:→ sarafciel: 更正 全特化 03/29 18:19
3F:推 Schottky: 楼下 Su22 03/29 19:18
4F:→ harryooooooo: c++17以後的话也可以用if constexpr 03/29 19:44
5F:→ jack7775kimo: 错误讯息里面为何会有vector<int>? 03/29 21:49
因为我只列出几个
※ 编辑: su27 (125.228.53.160 台湾), 03/30/2022 20:10:57
6F:推 steve1012: If branch 在runtime 才会跑所以template 还是instant 03/31 05:26
7F:→ steve1012: iate 那段code 了 03/31 05:26
8F:→ steve1012: 你要conditionally compile 要用constexpr if 03/31 05:26