所以我有一个小课,我想在其中添加结构化绑定支持。但是,我无法弄清楚如何专门化std::tuple_element
和std::tuple_size
使用我的模板类。这是我的尝试:
template<typename... Cmps>
struct CmpGroup
{
std::array<void*, sizeof...(Cmps)> cmps;
template<typename C>
C& get()
{
constexpr int index = GetIndexInPack<C, Cmps...>::value;
static_assert(index != -1);
return *static_cast<C*>(index);
}
template<size_t I>
auto& get()
{
static_assert(I < sizeof...(Cmps));
using CmpType = typename GetTypeInPack<I, Cmps...>::type;
return *static_cast<CmpType*>(cmps[I]);
}
};
namespace std
{
template<typename... Types>
struct tuple_size<CmpGroup<Types...>> : public integral_constant<size_t, sizeof...(Types)>;
template<std::size_t N, typename... Types>
struct tuple_element<N, CmpGroup<Types...>> {
//got this from: https://blog.tartanllama.xyz/structured-bindings/
using type = decltype(std::declval<CmpGroup<Types...>>().template get<N>());
};
}
(为了清楚起见,我省略了一些实现特定的东西,完整的片段可以在这里找到)
但是,这会导致 tuple_size 特化给出编译错误:error C2143: syntax error: missing ',' before ';'
甚至可以允许模板类具有结构化绑定还是我遗漏了什么?