简短的问题:我可以输入一个可变参数包吗?我需要template <typename ...T> struct Forward { typedef T... args; };.
长版:
我正在考虑重新实现C++ 0x中出色的boost bimap.回想一下两种类型的bimap S并且T是和之间std::set的关系.对象本身存储在两个独立的内部容器中,关系跟踪我认为的相关迭代器; 两种类型都可以通过"左"和"右"查找作为键.根据内部容器的选择,值可以是唯一的,例如,如果左容器是一个集合而右容器是多集合,则可以映射到许多不同的s,右查找给出相等的范围.大众内部容器,,和,也许版本了.S xT yxysetmultisetvectorlistunordered_*
所以我们需要一个接受两个容器作为模板参数的类型:
class Bimap<S, T, std::set, std::multiset>
Run Code Online (Sandbox Code Playgroud)
但我们必须接受容器可以采用任意多个参数,因此我们也需要传递所有这些参数.如果我们只需要一组可变参数,那就不会有问题,因为我们可以直接传递它们.但是,我们现在需要2套的参数,所以我想写一个转发器,使用像这样:
Bimap<int, int, std::set, std::set, Forward<std::less<int>, MyAllocator>, Forward<std::greater<int>, YourAllocator>> x;
Run Code Online (Sandbox Code Playgroud)
这是我提出的模板:
#include <set>
#include <cstdint>
template <typename ...Args>
struct Forward
{
typedef Args... args; // Problem here!!
static const std::size_t size = sizeof...(Args);
};
template <typename S, typename T,
template <typename ...SArgs> class SCont,
template <typename ...TArgs> class TCont,
typename SForward = Forward<>, typename TForward = Forward<>>
class Bimap
{
typedef SCont<S, typename SForward::args> left_type;
typedef TCont<T, typename TForward::args> right_type;
template <typename LeftIt, typename RightIt> struct Relation; // to be implemented
typedef Relation<typename left_type::const_iterator, typename right_type::const_iterator> relation_type;
};
int main()
{
Bimap<int, int, std::set, std::set, Forward<std::less<int>>, Forward<std::greater<int>>> x;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,在指示的行中,Forward我无法弄清楚如何键入参数包!(注释行给出了编译器错误.)
[我想我可以去一个懒惰的版本Bimap<std::set<int, MyPred>, std::multiset<char, YourPred>> x;,并通过提取类型LeftCont::value_type和RightCont::value_type,但我想那会更好,如果我能做出的密钥类型我的主模板参数,并允许默认为std::set容器.]
您可以通过将variadic参数包封装在元组中以及稍后使用以下两个帮助器模板结构来转发实际的可变参数来实现您的目的:
template<typename PackR, typename PackL>
struct cat;
template<typename ...R, typename ...L>
struct cat<std::tuple<R...>, std::tuple<L...>>
{
typedef std::tuple<R..., L...> type;
};
Run Code Online (Sandbox Code Playgroud)
和
template<typename Pack, template<typename ...T> class Receiver>
struct Unpack;
template<typename ...Args, template<typename ...T> class Receiver>
struct Unpack<std::tuple<Args...>, Receiver>
{
typedef Receiver<Args...> type;
};
Run Code Online (Sandbox Code Playgroud)
您的代码示例如下所示:
#include <set>
#include <cstdint>
#include <tuple>
template<typename PackR, typename PackL>
struct Cat;
template<typename ...R, typename ...L>
struct Cat<std::tuple<R...>, std::tuple<L...>>
{
typedef std::tuple<R..., L...> type;
};
template<typename Pack, template<typename ...T> class Receiver>
struct Unpack;
template<typename ...Args, template<typename ...T> class Receiver>
struct Unpack<std::tuple<Args...>, Receiver>
{
typedef Receiver<Args...> type;
};
template<typename ...Args>
struct Forward
{
//typedef Args... args; // Problem here!!
typedef std::tuple<Args...> args; // Workaround
static const std::size_t size = sizeof...(Args);
};
template<typename S, typename T,
template<typename ...SArgs> class SCont,
template<typename ...TArgs> class TCont,
typename SForward = Forward<> ,
typename TForward = Forward<>>
class Bimap
{
//typedef SCont<S, typename SForward::args> left_type;
//typedef TCont<T, typename TForward::args> right_type;
typedef typename Unpack<typename Cat<std::tuple<S>, typename SForward::args>::type, SCont>::type left_type; //Workaround
typedef typename Unpack<typename Cat<std::tuple<T>, typename TForward::args>::type, TCont>::type right_type; //Workaround
template<typename LeftIt, typename RightIt> struct Relation; // to be implemented
typedef Relation<typename left_type::const_iterator, typename right_type::const_iterator> relation_type;
};
int main()
{
Bimap<int, int, std::set, std::set, Forward<std::less<int>> , Forward<std::greater<int>>> x;
}
Run Code Online (Sandbox Code Playgroud)
在gcc 4.6.0下编译得很好
| 归档时间: |
|
| 查看次数: |
1864 次 |
| 最近记录: |