c++ - 验证模板参数是其他定义的模板

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

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

template <class... Pack>
class Foo
{

    class SomeClass {};

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

假定不可能进行欺骗。

最佳答案

部分特化,就像这样:

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 是不完整的类型。我希望这就是您所追求的。

关于c++ - 验证模板参数是其他定义的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22438758/

相关文章:

ruby - 定义类的不同方式如何影响包含作品的方式?

c++ - 通过 FLASCC 从 C++ 到 Actionscript

c++ - CUDA素数生成

c++ - STL map 容器在构造时是否优化(平衡树)?

c++ - 隐式转换为 std::vector

c++ - 如何在构造函数的初始化列表中初始化共享指针?

c++ - VS 2013 SFINAE 缺陷的解决方法

c++ - 整理私有(private)函数的元编程技巧

c++ - 模板类根据它们的存在和优先级调用其他类的一些命名函数

c++ - 如何使用类中的线程操作 vector ?