If I have a template function, how can I restrict this function to only two types, without using type_traits
?
#include <iostream>
#include <utility>
#include <type_traits>
template <typename T>
void swap_bytes(T& a, T& b)
{
std::swap(a, b);
}
template <>
void swap_bytes(int& a, int& b)
{
std::swap(a, b);
}
template <>
void swap_bytes(float& a, float& b)
{
std::swap(a, b);
}
int main()
{
int a{ 3 }, b{ 4 };
swap_bytes(a, b);
char c1{ 'q' }, c2{ 'r' };
swap_bytes(c1, c2); // works ! and it shouldn't !
return 0;
}
As you see, I still can use this function with char
type.