I was thinking to be more pedantic in choosing data type in a block of code where I would need to choose between size_typesize_t in general or container::size_type for container types. My problem is that if I have the following block of code, I don't know how to do it. Can anybody there help?
template<typename some_container>
int func(some_container& input)
{
//Some code...
//...
decltype(input.size()) variable_x; //Choose this if defined,
size_t variable_x; //otherwise choose this
//... Some more code...
}
In this case some_container may be a custom container and does not provide size() function. What led me to this thinking was reading the difference between size_t and container::size_type at size_t vs container::size_type. I also read Determine if a type is an STL container at compile time, but the approach feels a bit heavy-handed for my situation.
You are on the right way using decltype, the trick is to use SFINAE which is easily done using either template classes or functions overloads. I will show the function way since it's so easy in C++11:
// One helper per size_type source
template <typename T>
auto size_type_alt(T const& t) -> decltype(t.size());
auto size_type_alt(...) -> std::size_t;
// The switch between helpers
template <typename T>
auto size_type_switch() -> decltype(size_type_alt(std::declval<T>{}));
// And syntactic sugar
template <typename T>
using size_type = decltype(size_type_switch<T>());
Usage:
template <typename T>
void some_algorithm(T const& t) {
size_type<T> const size = 0;
// ...
}
Note: the switch and sugar coating layer could be blended together, however I thought you might appreciate seeing the steps separately.
Following is one way to determine if a class contains a type (e.g. size_type) or not:
template <typename T>
struct Has_size_type
{
typedef char (&yes)[2];
template <typename C> static yes test(typename C::size_type*);
template <typename> static char test(...);
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};
And following is the way to choose between 2 types:
template<bool> struct Bool;
template<typename T, typename = Bool<true> >
struct Set { typedef size_type type; };
template<typename T>
struct Set<T,Bool<Has_size_type<T>::value> > { typedef typename T::size_type type; };
Edit start: Here is another simpler approach:
template<typename T>
struct void_ { typedef void type; };
template<typename T, typename = void>
struct Set
{ typedef size_type type; };
template<typename T>
struct Set<T,typename void_<typename T::size_type>::type>
{ typedef typename T::size_type type; };
Edit end.
So finally, use as below:
template<typename some_container>
int func(some_container& input)
{
typedef typename Set<some_container>::type type;
}
So now type is either size_type or some_container::size_type, if it has that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With