9

I have is_string defined as follow in my C++ code:

#include <string>

template <typename T>
struct is_string
{
    static const bool value = false;
};

template <class T, class Traits, class Alloc>
struct is_string<std::basic_string<T, Traits, Alloc>>
{
    static const bool value = true;
};

int main()
{
    std::cout << is_string<std::string>::value << std::endl;
    std::cout << is_string<std::wstring>::value << std::endl;

    return 0;
}

It is true for both std::string and std::wstring.

But I need a predicate like this:

is_string<char, std::string>::value //to be true
is_string<char, std::wstring>::value //to be false
is_string<wchar_t, std::string>::value //to be false
is_string<wchar_t, std::wstring>::value //to be true

Is it possible to implement it?

1
  • 1
    Notice that you can inherit from std::true_type/std::false_type to shorten code.
    – Jarod42
    Commented Aug 15, 2018 at 19:14

4 Answers 4

8

Try this:

template <typename T, typename S>
struct is_string
{
  static const bool value = false;
};

template <class T, class Traits, class Alloc>
struct is_string<T, std::basic_string<T, Traits, Alloc>>
{
  static const bool value = true;
};

In this specific case, a simpler solution would be:

template<class T, class S>
using is_string = std::is_same<T, typename S::value_type>;

(However, it doesn't check that the second type is actually a string, if that is fine with you - this solution checks if the second type is any container that holds elements of the first type)

3
  • The alternative with std::is_same is not a good idea, because it can be true for std::vector<char>, for example. Commented Aug 15, 2018 at 16:31
  • 2
    your first solution is the simple and elegant one. Your last solution spoils an otherwise good answer. is_string<int, std::vector<int>> would be true
    – bolov
    Commented Aug 15, 2018 at 16:32
  • I used your first solution. I think it is the best one. Commented Aug 15, 2018 at 16:44
2

Firstly, you need to make is_string taking two template parameters.

template <typename T, typename = void>
struct is_string
{
    static const bool value = false;
};

template <class T, class Traits, class Alloc>
struct is_string<std::basic_string<T, Traits, Alloc>, void>
{
    static const bool value = true;
};

Then

template <class T, template <typename, typename, typename> class STRING>
struct is_string<T, STRING<T, std::char_traits<T>, std::allocator<T>>>
{
    static const bool value = true;
};

LIVE

4
  • Of the three pieces of code, you only need the last one, right?
    – anatolyg
    Commented Aug 15, 2018 at 16:28
  • You need the first one to avoid compile errors when you were to get false, but the second is only needed if you want the base version with one argument. However, this will not accept strings with non-default traits or allocator.
    – riv
    Commented Aug 15, 2018 at 16:29
  • @anatolyg I want to reserve the usage like is_string<std::string>::value. Commented Aug 15, 2018 at 16:30
  • @riv It depends on OP's intent; if necessary we can use parameter pack to make it applicable for non-default traits or allocator. Commented Aug 15, 2018 at 16:31
0

Maybe like this:

template<typename x_Char, typename x_String>
struct is_string
{
    static constexpr bool const value
    {
        (
            ::std::is_same_v<::std::string, x_String>
            or
            ::std::is_same_v<::std::wstring, x_String>
        )
        and
        ::std::is_same_v<typename x_String::value_type, x_Char>
    };
};

static_assert(true == is_string<char, std::string>::value); //to be true
static_assert(false == is_string<char, std::wstring>::value); //to be false
static_assert(false == is_string<wchar_t, std::string>::value); //to be false
static_assert(true == is_string<wchar_t, std::wstring>::value); //to be true

online compiler

To deal with allocators:

template<typename x_Char, typename x_String>
struct is_string
: ::std::false_type {};

template<typename x_Char, typename x_CharTrait, typename x_Allocator>
struct is_string<x_Char, ::std::basic_string<x_Char, x_CharTrait, x_Allocator>>
: ::std::true_type {};

online compiler

4
  • ::std::is_same_v<::std::string, x_String> - compares with a string that has the default allocator. What if the allocator is not default? Commented Aug 15, 2018 at 16:26
  • @AlexeyStarinsky From the question it looks like you don't care about allocators and only char type is relevant. Commented Aug 15, 2018 at 16:29
  • @AlexeyStarinsky Updated my answer to include case that will deal with allocators as well. Commented Aug 15, 2018 at 16:35
  • I don't think you should specifically test with std::string and std::wstring. The type of string should be left to the user of the template. We now have many more char types (like char32_t) so you could have many different types of strings, not just these two. Commented Mar 29, 2022 at 19:18
0

Here is a C++17 version.

namespace impl
{
    // decay_t will remove const, & and volatile from the type
    template<typename T>
    inline constexpr bool is_string_class_decayed = false;

    template<typename... T>
    inline constexpr bool is_string_class_decayed<std::basic_string<T...>> = true;
} // namespace impl

template<typename T>
inline constexpr bool is_string_class = impl::is_string_class_decayed<std::decay_t<T>>;

template <typename TChar, typename TString>
inline constexpr bool is_string = is_string_class<TString> && std::is_same_v<TChar, TString::value_type>;


// Compile-time tests, you don't even need to write unit-tests to know it all works as expected
static_assert(is_string_class<std::string>);
static_assert(is_string_class<const std::wstring&>); // that's why we need decay_t
static_assert(!is_string_class<int>);
static_assert(!is_string_class<const double>);
static_assert(!is_string_class<const char*>);
static_assert(!is_string_class<std::vector<char>>);

static_assert(is_string<char, std::string>);
static_assert(is_string<wchar_t, std::wstring>);
static_assert(!is_string<char, std::wstring>);
static_assert(!is_string<wchar_t, std::string>);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.