Compare variable

Hi, I have another problem.
Does anyone know how to compare variable in the template?

1
2
3
4
5
template <typename st>
st ConT(st x) {
!Problem!
return x;
}


I need compare if is x bool, string, int and other.
Have a look at the type_traits. http://www.cplusplus.com/reference/type_traits/
Does this mean you want to determine if st is one of bool, string, int? It is relatively easy to create a metafunction based off of std::is_same:

1
2
3
4
5
6
7
8
9
10
11
// Pre-c++17
template <typename T, typename U, typename... Rest> 
struct is_any_of: std::integral_constant<bool, is_any_of<T, U>::value || 
                                         is_any_of<T, Rest...>::value> {};
template <typename T, typename U>
struct is_any_of<T, U>: std::integral_constant<bool, std::is_same<T, U>::value> {};

// Post C++17
template <typename T, typename...Rest> 
static constexpr bool is_any_of_2()
{ return (std::is_same<T, Rest>::value || ...); }


If you simply want different definitions of st() for different types st, create function overloads for ConT():
1
2
3
template <typename st>
st ConT(st x)  { ... };
int ConT(int x) { ... };

Demo:
http://coliru.stacked-crooked.com/a/ad8dbdcead80bbfe

Last edited on
Topic archived. No new replies allowed.