下面的示例代碼說明我的問題:如何專門用可變參數類模板
#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();
}
我想知道我可以專注性狀結構,這樣的類型存儲器進行適當的分配爲標準::在上面的例子中,N設爲10。
你想static_vector的類型是什麼呢? –
你想如何在這裏推出'N'?爲什麼你需要一個可變模板而不是簡單的兩個參數? – Holt