3

是否有可能將兩個模板類傳遞到模板類中?模板類的多個模板模板參數

我期待創建一個持有兩個不同的類std::tuple<std::vector<>>

我開始懷疑我想達到的目標無法完成,但是我找不到任何其他的說法。

#include <iostream> 
#include <vector> 
#include <tuple> 

template<typename... Ts> 
struct Typelist 
{ 
    static constexpr std::size_t size { sizeof...(Ts) }; 
}; 

template<class, class> class World; 

template<template<typename... Arg1> class T1, template<typename... Arg2> class T2> 
class World<Typelist, Typelist> 
{ 

private: 
    std::tuple<std::vector<T1>...> m1; 
    std::tuple<std::vector<T2>...> m2; 
}; 


int main() { 
    // your code goes here 
    using TL1 = Typelist<int, char, double>; 
    using TL2 = Typelist<float, unsigned int, bool>; 

    World<TL1, TL2> w2; 
    return 0; 
} 

Live Example

這是可能的,如果是這樣,我在做什麼錯:

下面是代碼我一起工作? 如果沒有,是否有可能的替代方案?

+1

由於'Typelist'不是一個類,因此使用'World '是不正確的。你可以使用'World ,Typelist >',但是從你的文章中不清楚'args1'和'args2'的合理值。 –

+0

請向我解釋這個元組的用途是什麼,這對我來說一點都不清楚,因爲寫入的內部只有一種類型,而且你濫用'...'操作符?沒有參數包可以擴展?你想要做'std :: tuple ...>'? – OmnipotentEntity

回答

2

這是我認爲你的意思。由

#include <iostream> 
#include <vector> 
#include <tuple> 

template<typename... Ts> 
struct Typelist 
{ 
    static constexpr std::size_t size { sizeof...(Ts) }; 
}; 

template <class, class> 
class World; 

template <typename... Arg1, typename... Arg2> 
class World<Typelist<Arg1...>, Typelist<Arg2...>> 
{ 

    private: 
    std::tuple<std::vector<Arg1>...> m1; 
    std::tuple<std::vector<Arg2>...> m2; 
}; 


int main() { 
    using TL1 = Typelist<int, char, double>; 
    using TL2 = Typelist<float, unsigned int, bool>; 

    World<TL1, TL2> w2; 
    return 0; 
} 

的變化:

首先,模板特改爲兩個裸參數組,這是可行的,因爲參數組是由模板匹配引擎基礎上,類型與Typelist填寫,所以它是毫不含糊的。您無法使用先前使用的規範,因爲您只能訪問T1T2,因此您無權訪問內部參數包參數的名稱。第二,我改變了如何定義World的數據成員,以便m1m2是類型向量的元組。

第三,你可以完全擺脫Typelist並直接使用tuple。除非它在做這個代碼沒有特色的東西。

#include <iostream> 
#include <vector> 
#include <tuple> 

template <class, class> 
class World; 

template <typename... Arg1, typename... Arg2> 
class World<std::tuple<Arg1...>, std::tuple<Arg2...>> 
{ 

    private: 
    std::tuple<std::vector<Arg1>...> m1; 
    std::tuple<std::vector<Arg2>...> m2; 
}; 


int main() { 
    using TL1 = std::tuple<int, char, double>; 
    using TL2 = std::tuple<float, unsigned int, bool>; 

    World<TL1, TL2> w2; 
    return 0; 
}