【问题标题】:Validate template parameter is other defined template验证模板参数是其他定义的模板
【发布时间】:2014-03-16 15:23:10
【问题描述】:

如何测试 OtherFoo 模板参数(对于我的 TT 别名)是 Foo 与其他模板参数:

template <class... Pack>
class Foo
{

    class SomeClass {};

    template <class OtherFoo> // OtherFoo = Foo with other template parameters
    using TT = typename OtherFoo::SomeClass;
};

假设不可能进行欺骗。

【问题讨论】:

    标签: c++ c++11 metaprogramming template-meta-programming


    【解决方案1】:

    部分特化,像这样:

    template <class... Pack>
    class Foo
    {
        class SomeClass {};
    
        template <class OtherFoo>  // OtherFoo = anything, but undefined
        struct TT_t;
    
        template <class... P> // OtherFoo = Foo with other template parameters
        struct TT_t <Foo <P...> > { using type = typename Foo <P...>::SomeClass };
    
        template <class OtherFoo> // OtherFoo = Foo with other template parameters
        using TT = typename TT_t <OtherFoo>::type;
    };
    

    因此,如果您尝试使用除另一个Foo 之外的任何参数来实例化TT,编译器将发出错误,指出TT_t 是不完整的类型。我希望这就是你所追求的。

    【讨论】:

    • 看来行得通。你能解释一下这种情况下的偏特化是怎么回事吗?
    • 我不确定我是否听懂了你的问题。无论如何,一般X 是用任意参数OtherFoo 声明的,但没有这样定义。仅在OtherFoo 的形式为Foo &lt;P...&gt; for some 参数P... 的情况下才定义特化,如果是,则会自动推导出。在这种情况下,Foo &lt;P...&gt; 当然是OtherFoo,所以通过附加::SomeClass,我们只需“读取”其嵌套类SomeClass(保证所有Foo 都有)。我希望这更清楚一点。你可以做一些阅读,例如here.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 2018-03-24
    • 2011-12-22
    • 1970-01-01
    • 2018-07-04
    • 2018-12-16
    相关资源
    最近更新 更多