Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 更改可变模板中的最后一种类型_C++ - Fatal编程技术网

C++ 更改可变模板中的最后一种类型

C++ 更改可变模板中的最后一种类型,c++,C++,实际上,我发现了类似的问题,但都与函数参数包有关,并通过std::tuple和std::index_序列处理它们。我需要一些不同的东西,我自己无法连接。这是: 假设我有一个函数,取一些变量包,我想实现: template <typename NewVal, template Container, typename T, typename... Ts> auto foo(Container<T, Ts...> t, NewVal const& v) { us

实际上,我发现了类似的问题,但都与函数参数包有关,并通过
std::tuple
std::index_序列
处理它们。我需要一些不同的东西,我自己无法连接。这是:

假设我有一个函数,取一些变量包,我想实现:

template <typename NewVal, template Container, typename T, typename... Ts>
auto foo(Container<T, Ts...> t, NewVal const& v) {
    using OutContainer= Container<NewVal, Same as Ts up to last Type..., but LastElement should be different>
    OutContainer o;
    // fill this new container with v
    return o;
}
模板
自动foo(集装箱t、新集装箱和v){
使用OutContainer=Container
外箱o;
//把这个新容器装满v
返回o;
}
我观察到,在STL中,
Allocator
容器中的模板参数总是最后一个,存储的类型总是第一个(现在不要介意
std::map
)。因此,如果我想将一个
容器
变成一个
容器
,我还需要处理最后一个分配器参数,以便为这个新类型使用分配器。有什么想法吗


这当然是一种伪代码,
Container
是一个模板参数。

std::make\u index\u sequence
std::index\u sequence\u for
馈送给助手结构或函数通常是对包执行非琐碎操作的最简单方法

要创建具有所需类型转换的helper结构,请首先使用必要的输入声明它,再加上一个额外的类型参数以获取
std::index_序列

namespace foo_detail
{
    template <template <typename...> class Container,
              typename NewFirstT,
              typename NewLastT,
              typename IdxSeq,
              typename ... Tail>
    struct OutContainerHelper;
}
(额外的
T2
确保
容器
专门化至少有两个模板参数,因为转换在其他情况下没有意义。如果有人试图将编译错误与只有一个模板参数的模板一起使用,这可能会使编译错误稍微不那么混乱。)

namespace foo_detail
{
    template <template <typename...> class Container,
              typename NewFirstT,
              typename NewLastT,
              std::size_t ... I,
              typename ... Tail>
    struct OutContainerHelper<
        Container, NewFirstT, NewLastT, std::index_sequence<I...>, Tail...>
    {
        using type = Container<
            NewFirstT,
            std::conditional_t<(I+1 == sizeof...(Tail)), NewLastT, Tail>...
        >;
    };
}
template <template <typename...> class Container,
          typename OldFirstT,
          typename T2,
          typename... Ts,
          typename NewVal>
auto foo(Container<OldFirstT, T2, Ts...> t, NewVal const& v) {
    using NewLastT = ???;
    using OutContainer = typename foo_detail::OutContainerHelper<
        Container, NewVal, NewLastT,
        std::index_sequence_for<T2, Ts...>, T2, Ts...
    >::type;
    OutContainer o;
    // fill this new container with v
    return o;
}