递归树数据结构的编译时类型检查



目标:我正在实现一个树数据结构,该结构将使用递归接口函数。我的树由节点和叶节点组成。节点由元数据和子节点组成,而叶节点是简单数据类型(string、int、float…(的别名

实现:让我们考虑接口函数的一个实例,即"读取";作用此函数根据给定的ID序列遍历树并返回存储的数据。

template<class T, class... Args>
T read(const uint8_t& ID, const Args&... residualIDs){
T ret;

//Generic compile-time implementation of switch-case
if constexpr (sizeof... (residualIDs) > 0)
{
initializer_list<bool> {
N::Header::guard(ID) ? (ret = get<id2idx<N::Header::ID>::getIndex()>(childs).template
read<T>(residualIDs...),
false) : false... 
};
}
using dataType = decltype(get<0>(get<0>(childs).childs));
if constexpr((sizeof... (residualIDs) == 0) && (is_same_v<T,dataType>))
{
cout << "Extract" << endl;
initializer_list<bool> {
N::Header::guard(ID) ? ( ret = get<0>(get<id2idx<N::Header::ID>::getIndex()>(childs).childs),
false) : false... 
};
}

return move(ret);
}

错误/问题:但是,为了存储不同的数据类型,当查询的数据类型与给定的数据类型不匹配时,编译器应在最低级别(最低级别=原始数据存储(放弃读取命令。不幸的是,即使数据类型应该匹配,编译器也总是将读取命令作为条件丢弃

if constexpr((sizeof... (residualIDs) == 0) && (is_same_v<T,dataType>))

总是错误的。

如果数据类型不匹配,则条件为false,如果数据类型匹配,则情况为true。对于两种匹配的数据类型,我在执行时得到了相同的打印结果

cout << typeid(variable).name() << endl;

然而,根据答案,可能有一种类型被标记为常量。为了检查这个常量,我还尝试使用

if constexpr((sizeof... (residualIDs) == 0) && (is_same_v<remove_const_t<T>, remove_const_t<dataType>>))

没有取得任何成功。

问题

  1. 为什么我会遇到这种行为?我很好奇,想知道为什么我的情况总是错误
  2. 如何解决这种错误行为

在偶然发现帖子后,我可以解决这个问题。显然,

using dataType = decltype(get<0>(get<0>(childs).childs))

返回引用类型。为了删除引用,我不得不使用

using dataType = remove_reference_t<decltype(get<0>(get<0>(childs).childs))>;

最新更新