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?
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 {};
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