I am new to c++ templates. I am writing a set of functions that can manipulate two different template classes. One needs to always be passed by value, the other must be passed by reference because it represents a huge amount of data.
Here is a simplified example. If the arg is tagged a ref type, I want the function signature to be defined as taking it by const ref.
template<bool B, typename, typename T2>
struct if_ {};
template<typename T1, typename T2>
struct if_<true, T1, T2> {
typedef T1 type;
};
template<typename T1, typename T2>
struct if_<false, T1, T2> {
typedef T2 type;
};
struct ByvalTypeTag {};
template<typename T>
class Byval : public ByvalTypeTag
{
T somedata;
};
struct ByrefTypeTag {};
template<typename T>
class Byref : public ByrefTypeTag
{
T somedata;
};
template<typename T>
void myfunc(typename if_<std::is_base_of<ByrefTypeTag, T>::value, const T&, T>::type arg)
{
}
int _tmain(int argc, _TCHAR* argv[])
{
Byref<int> arg;
myfunc( arg );
return 0;
}
The error I get is:
error C2783: 'void myfunc(if_::value,const T&,T>::type)' : could not deduce template argument for 'T'
Maybe this is the wrong way of going about it. If possible I am trying to cut down on the number of relatively duplicate templates I am writing for the same function.