1
구조화 된 바인딩 지원을 추가하려는 클래스가 있습니다. 그러나 템플릿 기반 클래스로 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>());
};
}
(나는 명확성에 대한 몇 가지 구현 고유의 물건을 떠난, 전체 조각이
here을 찾을 수 있습니다)
그러나 이것은 컴파일 오류를 제공하기 위해 tuple_size 전문화가 발생합니다 : 이 내 시도 error C2143: syntax error: missing ',' before ';'
템플릿 기반 클래스가 구조화 된 바인딩을 갖도록 허용 할 수 있습니까, 아니면 내가 누락 된 항목이 있습니까?
구조체가 비어 있어도'{} '본문이 필요합니다. –
@BoPersson 거룩한 똥 나는 내가 그것을 놓쳤다라고 생각할 수 없다! 나는이 코드를 몇 시간 동안보고 있었다. – FireFlyForLife