lundi 28 décembre 2020

Error C2440 '

I am trying to iterate over an std::tuple and retrieve the true amount of space (bytes) it takes up. To handle std::string I need to use the .length() function but I am unable to compile, and I get the following error:

Error C2440 '': cannot convert from 'char' to 'std::string'

I have also tried using std::is_same (as commented) but without success. The function iterate_tuple_types is called like so:

tuple <char, unsigned int, double, std::string> src;
src = make_tuple('a', 10, 0.333333333333333, std::string("some string"));
iterate_tuple_types(src);

Code

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

template<int N, typename... Ts> using NthTypeOf =
    typename std::tuple_element<N, std::tuple<Ts...>>::type;

template <size_t I = 0, typename... Ts>
typename std::enable_if<I == sizeof...(Ts), size_t>::type
    iterate_tuple_types(std::tuple<Ts...> tup) {
    return 0;
}

template <size_t I = 0, typename... Ts>
typename  std::enable_if<(I < sizeof...(Ts)), size_t>::type
    iterate_tuple_types(std::tuple<Ts...> tup) {
    size_t result = 0;
    // std::is_same<std::string, NthTypeOf<I, Ts...> >::value
    if (std::is_integral<NthTypeOf<I, Ts...> >::value) {
        result += sizeof(NthTypeOf<I, Ts...>);
    }
    else if (is_string<std::string, NthTypeOf<I, Ts...> >::value) {
        result += static_cast<size_t>(std::string(std::get<I>(tup)).length());
    }
    return result + iterate_tuple_types<I + 1>(tup);
}

Am I going about this correctly? Why does the error occur?

Aucun commentaire:

Enregistrer un commentaire