Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type trait for strings

Is there an existing (in the standard library or in Boost) type trait to test whether a type could represent a string?

I stumbled upon an issue when using Boost.Fusion:

auto number = fusion::make_vector( 1, "one" );
auto numberName = fusion::filter< char const * >( number );

assert( numberName == fusion::make_vector( "one" ) ); // fails

I hoped filter would retain "one", but it failed because "one" is not decayed to a pointer (make_vector takes its arguments by reference, so the type is const char (&)[4]). Consequently, I need a trait that would allow me to write something like this:

auto numberName = fusion::filter_if< is_string< mpl::_ > >( number );

I am aware that a char const * and a const char[N] are not necessarily null-terminated strings, but it would still be handy to be able to detect them uniformly. The trait could also possibly return true for std::string and the likes.

Does such a trait exist or will I have to write my own?

like image 848
Luc Touraille Avatar asked Nov 11 '11 17:11

Luc Touraille


1 Answers

I gave a shot at implementing such a trait, but I am not sure it is really robust. Any input will be appreciated.

template <typename T>
struct is_string
    : public mpl::or_< // is "or_" included in the C++11 library?
        std::is_same<       char *, typename std::decay< T >::type >,
        std::is_same< const char *, typename std::decay< T >::type >
     > {};

assert ( ! is_string< int >::value );

assert (   is_string< char       *       >::value );
assert (   is_string< char const *       >::value );
assert (   is_string< char       * const >::value );
assert (   is_string< char const * const >::value );

assert (   is_string< char       (&)[5] >::value );
assert (   is_string< char const (&)[5] >::value );

// We could add specializations for string classes, e.g.
template <>
struct is_string<std::string> : std::true_type {};
like image 122
Luc Touraille Avatar answered Oct 15 '22 18:10

Luc Touraille