How to avoid {} when using aggregate initialization with empty base class

前端 未结 1 1702
遇见更好的自我
遇见更好的自我 2021-01-17 21:08

C++17\'s aggregate initialization for base class is awesome, but it is verbose when the base is only there to provide some functions (so no data members).

Here is mi

相关标签:
1条回答
  • 2021-01-17 21:37

    You can still provide constructor, for example:

    template <typename T, std::size_t N> using always_t = T;
    
    struct base_pod
    {
        // functions like friend compare operator
    };
    template<typename T, typename Seq> struct der_pod_impl;
    
    template<typename T, std::size_t ... Is>
    struct der_pod_impl<T, std::index_sequence<Is...>> : base_pod
    {
        der_pod_impl(always_t<T, Is>... args) : k{args...} {}
    
        T k[sizeof...(Is)];
    };
    
    template<typename T, std::size_t N>
    using der_pod = der_pod_impl<T, std::make_index_sequence<N>>;
    

    Demo

    0 讨论(0)
提交回复
热议问题