Objective: I am implementing a tree data structure which shall make use of recursive interface functions. My tree is composed of nodes and leaf nodes. Nodes are composed of metadata and child nodes, whereas leaf node is an alias for a simple data type (string, int, float, ...).
Implementation: Let's consider one instance of the interface functions, namely the "read" function. This function traverse the tree in accordance to the given ID-sequence and returns the stored data.
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);
}
Error/Problem: However, in order to store varying data types, the compiler shall discard the read-command at the lowest level (lowest level = primitive data storage) when the queried data type does not match the given data type. Unfortunately, even if the data type should match, the compiler always discards the read-command as the condition
if constexpr((sizeof... (residualIDs) == 0) && (is_same_v<T,dataType>))
is always false.
I would expect that the condition is false if the data types do not match and that the condition is true if they match. For two matching data types, I got the same print-result when executing
cout << typeid(variable).name() << endl;
However, according to the answer it could be that one type is tagged with a const. To check on this const, I have also tried to use
if constexpr((sizeof... (residualIDs) == 0) && (is_same_v<remove_const_t<T>, remove_const_t<dataType>>))
without any success.
Questions:
- Why do I encounter this behavior? I am curious and want to know why my condition is always false
- How could this erroneous behavior be solved?