テンプレートクラスのコンテナタイプを検出し、そのパラメータを再定義できるかどうか疑問に思っていました。例:コンテナテンプレートクラスをC++で抽出することはできますか?
typedef std::vector<int> vint;
typedef typeget<vint>::change_param<double> vdouble;
ここで、vdoubleはstd::vector<double>
になりますか?
テンプレートクラスのコンテナタイプを検出し、そのパラメータを再定義できるかどうか疑問に思っていました。例:コンテナテンプレートクラスをC++で抽出することはできますか?
typedef std::vector<int> vint;
typedef typeget<vint>::change_param<double> vdouble;
ここで、vdoubleはstd::vector<double>
になりますか?
は、ここでは一般的なアプローチです。
はい、あなたは部分的な特殊化を使用してレビンダー単純なテンプレート加えることができます。
#include <memory>
#include <vector>
template <typename> struct vector_rebinder;
template <typename T, typename A>
struct vector_rebinder<std::vector<T, A>>
{
template <typename U>
using rebind =
std::vector<U,
typename std::allocator_traits<A>::template rebind_alloc<U>>;
};
使用法:今すぐ
using T1 = std::vector<int>;
using T2 = vector_rebinder<T1>::rebind<double>;
T2
がstd::vector<double>
です。
template<typename...> struct rebinder;
template<template<typename...> class Container, typename ... Args>
struct rebinder<Container<Args...>>{
template<typename ... UArgs>
using rebind = Container<UArgs...>;
};
太陽の下で任意の容器のために動作します:@Kerrek SBの答えへに追加
悲しいかな、これはベクタだけで動作します。テンプレートクラス、リスト、マップ、マイリストなどのために、それぞれのリバインダがなくても構いません。 – g24l
一つの問題は、ベクトルの型を変更しますが、アロケータの型は変更しないことです。あなたのベクトルは、最初に 'int'型の値と' std :: allocator
@Pumkko:ああ、それは確かにひどい考えです。私はそれを変更させてください。 –
なぜstd :: arrayで動作しないのですか? –
@Benoîtは値のテンプレートパラメータを持っているため、このクラスはこの場合に変更する必要があります。 – CoffeeandCode
Hum。typename ...がタイプにしか拡張されていないという事実については一度も分かりませんが、これは明らかです。ありがとうございました。 –