Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting basic_string instantiation

I wrote the following code to determine if a type is an instantiation of std::basic_string:

template <typename T>
struct is_string
{
    enum { value = false };
};

template <typename charT, typename traits, typename Alloc>
struct is_string<std::basic_string<charT, traits, Alloc> >
{
    enum { value = true };
};

Is there a more succinct way to achieve that?

like image 270
fredoverflow Avatar asked Nov 05 '22 02:11

fredoverflow


1 Answers

Okay, I found a slightly shorter way:

#include <type_traits>

template <typename T>
struct is_string : std::false_type {};

template <typename charT, typename traits, typename Alloc>
struct is_string<std::basic_string<charT, traits, Alloc> > : std::true_type {};

But maybe others can do even better? :)

like image 92
fredoverflow Avatar answered Nov 09 '22 05:11

fredoverflow