2017-12-27 3 views
1

구조화 된 바인딩 지원을 추가하려는 클래스가 있습니다. 그러나 템플릿 기반 클래스로 std::tuple_elementstd::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 ';'

템플릿 기반 클래스가 구조화 된 바인딩을 갖도록 허용 할 수 있습니까, 아니면 내가 누락 된 항목이 있습니까?

+2

구조체가 비어 있어도'{} '본문이 필요합니다. –

+0

@BoPersson 거룩한 똥 나는 내가 그것을 놓쳤다라고 생각할 수 없다! 나는이 코드를 몇 시간 동안보고 있었다. – FireFlyForLife

답변

3

내가 알기로, 내 tuple_size 전문화에는 내가 간과 한 신체가 필요했습니다.

template<typename... Types> 
struct tuple_size<CmpGroup<Types...>> : public integral_constant<size_t, sizeof...(Types)>{}; 
------------------------------------------------------------------------------------------^^ 

고맙습니다. @BoPersson이 지적했습니다.

관련 문제