5

各クラステンプレート/クラスに特化せずに、一般化された「再バインド」メタ関数を書くことは可能ですか?

 template<class > struct foo;
 struct bar;

以下

 is_same<rebind<foo<int>,float>,foo<float>> 
 is_same<rebind<bar>,bar>

そして多分

is_same< rebind<std::vector<int>,float>,std::vector<float>>  

true と同等の型を返しますか?

4

1 に答える 1

6

もちろん。

ただし、可変個引数のテンプレート パラメーター リストを使用するテンプレート テンプレート パラメーターは、型パラメーター以外のパラメーターではなく、型パラメーターのみを持つテンプレートを受け入れるように制限されていることに注意してください。std::arrayつまり、次の一般的なケースは、2 番目の引数が整数であるため機能しません。特別なケースを追加する必要があります。

プライマリ テンプレートは、テンプレートの特殊化ではないクラスを処理するため、既に特殊なケースです。

http://liveworkspace.org/code/5b6f0cb3aec1eb74701e73d8d21aebab

template< typename bound, typename ... new_args >
struct rebind_class {
    static_assert( sizeof ...( new_args ) == 0,
         "can't rebind arguments to non-specialization" );
    typedef bound type;
};

template< template< typename ... > class template_, typename ... new_args,
    typename ... old_args >
struct rebind_class< template_< old_args ... >, new_args ... > {
    typedef template_< new_args ... > type;
};

template< typename ... args >
using rebind = typename rebind_class< args ... >::type;
于 2012-11-08T07:45:10.387 に答える