I have the following meta struct templates, which are intended to check whether a specific type (the KeyType
) is part of a parameter pack.
#include <type_traits>
#include <utility>
template<typename ...>
struct find_type : std::false_type {};
//specialization used if queried type is found
template<typename QueriedType>
struct find_type<QueriedType, QueriedType> : std::true_type {};
//specialization used if KeyType and ValueType not wrapped
template<typename QueriedType,
typename KeyType,
typename ValueType,
typename... Remaining>
struct find_type<QueriedType, KeyType, ValueType, Remaining...>
{
static const bool value =
std::conditional<find_type<QueriedType, KeyType>::value,
std::true_type,
find_type<QueriedType, Remaining...>>::type::value;
};
//specialization used if KeyType and ValueType are wrapped inside e.g. std::pair
template<typename QueriedType,
template<typename, typename> class C,
typename KeyType,
typename ValueType,
typename ...Remaining>
struct find_type<QueriedType, C<KeyType, ValueType>, Remaining...> :
find_type<QueriedType, KeyType, ValueType, Remaining...>{};
//entry point
template<typename ...>
struct entry_find_type;
template<typename QueriedType,
typename ...Remaining>
struct entry_find_type<QueriedType, std::tuple<Remaining...>> :
find_type<QueriedType, Remaining...> {};
There are probably better ways to do this, but I got an interesting behavior when using the structs. Consider the following code snippet. In the first scope I trigger the find_type
first passing a key-value-pair without a wrapper followed by passing the second key-value-pair using std::pair
. This compiles fine. In the second scope I trigger the find_type
with the same key-value-pairs, just switching std::pair
with the unwrapped key-value-pair. This yields a compiler error due to ambiguous specializations (see below).
class IFirstType
{
};
class ISecondType
{
};
class IDerived : public IFirstType, public ISecondType
{
};
int main(int argc, char*argv[])
{
{ //compiles fine
typedef std::tuple<IFirstType, IDerived,
std::pair<ISecondType, IFirstType>> tuple_type;
typedef entry_find_type<ISecondType, tuple_type> query_type;
static const bool bFound = query_type::value;
}
{ //compile error
typedef std::tuple<std::pair<ISecondType, IFirstType>,
IFirstType, IDerived> tuple_type;
typedef entry_find_type<ISecondType, tuple_type> query_type;
//static const bool bFound = query_type::value; //<--- compiler error here
}
return 0;
}
The compiler output (gcc 4.9):
/tmp/gcc-explorer-compiler115628-35-8rkb3f/example.cpp: In instantiation of 'struct entry_find_type<ISecondType, std::tuple<std::pair<ISecondType, IFirstType>, IFirstType, IDerived> >':
69 : required from here
39 : error: ambiguous class template instantiation for 'struct find_type, IFirstType, IDerived>'
struct entry_find_type<QueriedType, std::tuple<Remaining...>> :
^
16 : error: candidates are: struct find_type
struct find_type<QueriedType, KeyType, ValueType, Remaining...>
^
30 : error: struct find_type, Remaining ...>
struct find_type<QueriedType, C<KeyType, ValueType>, Remaining...> :
^
39 : error: invalid use of incomplete type 'struct find_type, IFirstType, IDerived>'
struct entry_find_type<QueriedType, std::tuple<Remaining...>> :
^
5 : error: declaration of 'struct find_type, IFirstType, IDerived>'
struct find_type : std::false_type {};
^
/tmp/gcc-explorer-compiler115628-35-8rkb3f/example.cpp: In function 'int main(int, char**)':
69 : error: 'value' is not a member of 'query_type {aka entry_find_type, IFirstType, IDerived> >}'
static const bool bFound = query_type::value; //<--- compiler error here
^
Compilation failed
live example at gcc.godbolt.org
If I understand correctly, the compiler should choose the same template specializations in both cases and yet the second case does not compile. I want the compiler to pick the specialized template template parameter version for both usages of std::pair
. Can someone tell me what I'm doing wrong?
Aucun commentaire:
Enregistrer un commentaire