简短的问题:我可以输入一个可变参数包吗?我需要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 = …Run Code Online (Sandbox Code Playgroud) 当将类型传递给要作为分配器的类时,C++ 03标准库使用简单的模板类型参数.这是可能的,因为模板在C++中的工作方式.但是,它并不是非常简单,您可能不知道类型定义应该是什么样子 - 特别是在非标准类型的情况下.
我认为使用适配器类instread可能是个好主意.我已经创建了一个示例来向您展示我的意思:
#ifndef HPP_ALLOCATOR_ADAPTOR_INCLUDED
#define HPP_ALLOCATOR_ADAPTOR_INCLUDED
#include <memory>
template<typename T>
struct allocator_traits;
template<typename T, class allocator_type = std::allocator<T>>
class allocator_adaptor;
template<>
struct allocator_traits<void>
{
typedef std::allocator<void>::const_pointer const_pointer;
typedef std::allocator<void>::pointer pointer;
typedef std::allocator<void>::value_type value_type;
};
template<typename T>
struct allocator_traits
{
typedef typename std::allocator<T>::const_pointer const_pointer;
typedef typename std::allocator<T>::const_reference const_reference;
typedef typename std::allocator<T>::difference_type difference_type;
typedef typename std::allocator<T>::pointer pointer;
typedef typename std::allocator<T>::reference reference;
typedef typename std::allocator<T>::size_type size_type;
typedef typename std::allocator<T>::value_type value_type;
};
template<class allocator_type>
class allocator_adaptor<void, allocator_type>
: public allocator_traits<void>
{ …Run Code Online (Sandbox Code Playgroud)