I have a program like: (please ignore how nonsensical it looks, it just a reproducible example )
using MyUInt = unsigned int;
using MyLInt = long int;
template<class T1, class T2>
T1 fn(const T2& arg){
return T1(arg);
}
int main() {
std::cout << fn<int, MyUInt>(1);
std::cout << fn<double, MyLInt>(1);
return 0;
}
is it possible to assign a default value to T2 based on T1 so I don't have to declare it manually? What are the alternatives?
I'm looking for something like
using MyUInt = unsigned int;
using MyLInt = long int;
template<class T1, class T2 = typename std::is_same<T1, int>::value ? MyUInt : MyLInt>
T1 fn(const T2& arg){
return T1(arg);
}
int main() {
std::cout << fn<int>(1);
std::cout << fn<double>(1);
return 0;
}
I'm using C++17.