0

我有一个 std::tuple 类型,需要检查一个字符串是否可以反序列化为其元素之一。元素的索引和字符串仅在运行时才知道。

我的解决方案(如下)几乎可以工作,但在使用时会产生编译器错误std::tuple_element,给出:

错误:无效使用不完整类型 'struct std::tuple_element<0ul, std::tuple&>

#include <sstream>
#include <tuple>

template<class Tuple, std::size_t N>
struct IsDeserializable {
  static void check(std::size_t idx, std::stringstream& in) {
    IsDeserializable<Tuple, N-1>::check(idx, in);
    if (idx == N-1) {
      typename std::tuple_element<N-1,Tuple>::type tmp;
      in >> tmp;
    }
  }
};

template<class Tuple>
struct IsDeserializable<Tuple, 1> {
  static void check(std::size_t idx, std::stringstream& in) {
    if (idx == 0) {
      typename std::tuple_element<0,Tuple>::type tmp;
      in >> tmp;
    }
  }
};

template<class Tuple>
bool is_deserializable(std::size_t idx, const std::string& val) {
  std::stringstream in;
  in << val;
  try {
    IsDeserializable<Tuple, std::tuple_size<Tuple>::value>::check(idx, in);
    return true;
  }
  catch(...) {return false;}
} 


// Example Usage
std::tuple<int,double,char> MyTuple;

assert(is_deserializable<MyTuple>(0,"4"));
assert(is_deserializable<MyTuple>(1,"56.5"));
assert(is_deserializable<MyTuple>(2,"h"));

assert(!is_deserializable<MyTuple>(0,"4.3"));
assert(!is_deserializable<MyTuple>(1,"Hello"));
assert(!is_deserializable<MyTuple>(2,"42"));
4

0 回答 0