如何使用其他模板参数测试 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/