简体   繁体   中英

How to specialize with a variadic class template

The following example code illustrates my problem:

#include <array>
#include <vector>
#include <iostream>
#include <type_traits>

namespace Vector
{
    namespace Intern
    {
        template <typename T1, typename ...T2>
        struct Traits;

        // How can I specialize here so that Memory is being assigned properly??
        template <typename T1, int N>
        struct Traits<T1, int> {
            static constexpr bool Static = 1;
            using Memory = std::array<T1, N>;
        };

        template <typename T1>
        struct Traits<T1> {
            static constexpr bool Static = 0;
            using Memory = std::vector<T1>;
        };
    }

    template <typename T1, typename ...T2>
    class Object
    {
        public :
            void printd()
            {
                std::cout << "Is Static: " << Traits::Static << std::endl;
            }
        private:
            using Traits = Intern::Traits<T1, T2...>;
            using Memory = typename Traits::Memory;

            Memory m_memory;
    };

    template <typename T1, typename ...T2>
    static auto Create(T2&& ...ln) -> decltype(auto)
    {
        return new Object<T1, T2...>();
    }
}

int main()
{
    auto static_vector = Vector::Create<int>(10);
    static_vector->printd();

    auto active_vector = Vector::Create<int>(  );
    active_vector->printd();
}

I would like to know how I can specialise the traits struct so that the type Memory is properly assigned as std::array with N set to 10 in the above example.

You can't use an integer directly, but you can wrap the integer into a type. This can be done using eg std::integral_constant :

template <typename T1, typename T2, int N>
struct Traits<T1, std::integral_constant<T2, N>> {
    static constexpr bool Static = 1;
    using Memory = std::array<T1, N>;
};

template <typename T1>
struct Traits<T1> {
    static constexpr bool Static = 0;
    using Memory = std::vector<T1>;
};


auto static_vector = Vector::Create<int, std::integral_constant<int, 10>>();

Keep it simple:

#include <array>
#include <vector>
#include <iostream>
#include <type_traits>

namespace Vector
{
    struct VariableSize {};
    template<std::size_t N> struct FixedSize {};

    template<typename T, std::size_t N>
    auto Create(FixedSize<N>)
    {
        return std::array<T, N>();
    }

    template<typename T, std::size_t N>
    auto Create(VariableSize)
    {
        return std::vector<T>();
    }
}

int main()
{
    auto static_vector = Vector::Create<int>(Vector::FixedSize<10>());

    auto active_vector = Vector::Create<int>(Vector::VariableSize());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM