t348575
t348575

Reputation: 753

Error C2440 '<function-style-cast>': cannot convert from 'char' to 'std::string'

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?

Edit

I am able to find the length of the string like shown below, but constructing a stringstream and flushing contents to it seems very inefficient?

typename std::tuple_element<I, std::tuple<Ts...> >::type test = std::get<I>(tup);
std::stringstream ss(test);
result += static_cast<size_t>(ss.str().length());

Upvotes: 1

Views: 1663

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52461

Something like this, perhaps:

template <typename T>
size_t LengthOf(const T&) { return sizeof(T); }

template <typename ... Params>
size_t LengthOf(const std::basic_string<Params...>& s) { return s.length(); }

template <typename Tup>
size_t iterate_tuple_types(const Tup& t) {
  auto sum_length = [](const auto&... args) {
      return (LengthOf(args) + ...);
  };
  return std::apply(sum_length, t);
}

Demo

Upvotes: 2

Related Questions