2

Base2 つのテンプレート パラメータを持つテンプレート クラスが必要です。特に、2 番目のパラメーターはテンプレート パラメーターです。CRTPDerivedから派生します。のようなBase基本クラスを生成したいのですが、生成する基本クラスは の実際の基本クラスと同じではありません。テンプレートを送信するにはどうすればよいですか?DerivedBase<Derived,Derived::second_tmpl>Derived

#include <type_traits>

template<typename T, template<typename>class U>
struct Base
{
    using type = Base<T,U>;
    using devired_type = T;
    template<typename V>
    using second_tmpl = U<V>;
    using second_type = second_tmpl<type>;
};

template<typename T>
struct Template
{
    using type = Template<T>;
};

struct Derived
    :public Base<Derived,Template>
{
};

//true
static_assert(
        std::is_same<
            Derived::second_type,
            Template<Base<Derived,Template>>>::value,
        "false");
//false
static_assert(
        std::is_base_of<
            Base<Derived,Derived::second_tmpl>,
            Derived
        >::value,
        "false");

template<typename T>
using Template2 = Template<T>;
//false
static_assert(
        std::is_same<
            Base<Derived,Template>,
            Base<Derived,Template2>
        >::value,
        "false");

元のテンプレートではなく、元のテンプレートと同じテンプレートを使用してください。判断は誤りです。

4

1 に答える 1

2

これらは、tempalte テンプレート引数の制限です。

テンプレート テンプレートの引数は、C++ では二流市民です:(

2番目のアサートは本当に読むべきです

static_assert(std::is_base_of<Base<Derived, Template>, Derived>::value, "false");

これはうまくいくでしょう。

3番目の問題(「開いているテンプレートを型定義できない」という事実)に対処するには、メタ関数にします。たとえばTemplateGen、以下のプログラムで:

Live On Coliru

#include <type_traits>

template <typename T, typename UGen>
struct Base {
    using type = Base<T, typename UGen::template type<T> >;
    using devired_type = T;

    template <typename V> using second_tmpl = typename UGen::template type<T> ;
    using second_type = second_tmpl<type>;
};

template <typename T>
struct Template {
    using type = Template<T>;
};

struct TemplateGen {
    template <typename T> using type = Template<T>;
};

struct Derived : public Base<Derived, TemplateGen> {
};

// true
static_assert(std::is_same<Derived::second_type, Template<Derived> >::value, "false");
// false
static_assert(std::is_base_of<Base<Derived, TemplateGen>, Derived>::value, "false");

using Template2 = TemplateGen;

// false
static_assert(std::is_same<Base<Derived, TemplateGen>, Base<Derived, Template2>>::value, "false");

int main(){}
于 2014-11-24T16:24:36.327 に答える