I have a function,in a library, that is a variadic template, and is used by a other programme.
1
A.hpp
class A {
template<typename Ret,typename ... Args>
static Ret f(int id,Args&& ... args);
};
#include "A.tpl"
A.tpl
template<typename Ret,typename ... Args>
Ret A::f(int id,Args&& ... args)
{
//do somthing with args and id
Ret ret;
/// do somthing with ret
return ret;
}
My problem is this one: If Ret is void, the code is not correct. So i try to build a specialization of f:
2
A.tpl
template<typename ... Args>
void A::f<void,Args ...>(int id,Args&& ... args)
{
//do somthing with args and id
return;
}
template<typename Ret,typename ... Args>
Ret A::f(int id,Args&& ... args)
{
//do somthing with args and id
Ret ret;
/// do somthing with ret
return ret;
}
But this code is not correct.
so I try to split the code:
3
A.hpp
class A {
template<typename Ret,typename ... Args>
static Ret f(int id,Args&& ... args);
template<typname Ret>
static Ret f2();
}
#include "A.tpl"
A.tpl
template<typename Ret,typename ... Args>
Ret A::f(int id,Args&& ... args)
{
//do somthing with args and id
return f2<Ret>();
}
template<typename Ret>
Ret A::f2()
{
Ret ret;
/// do somthing with ret
return ret;
}
A.cpp
template<>
void A::f2<void>()
{
return;
}
Now the code is ok, and my lib compile fine in a .so/dll.
But when I use f(...), only f2 from "A.tpl" is find by the compiler, and not f2 in the .so/dll (from the .cpp). So the code is not valid (again), because of ret declared as void.
So, if anyone have any idea to deal with this ...
Edit
Solution:
Do the 3 solution, and add in A.tpl
template<>
void A::f2<void>();