Reputation: 161
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.
Upvotes: 3
Views: 92
Reputation: 69864
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());
}
Upvotes: 1
Reputation: 171127
You can't use an integer directly, but you can wrap the integer into a type. This can be done using
e.g. 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>>();
Upvotes: 1