没有typedef的人可以访问模板外部的template参数吗? [英] Can one access the template parameter outside of a template without a typedef?
本文介绍了没有typedef的人可以访问模板外部的template参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个简单的例子:
template<typename _X> // this template parameter should be usable outside!
struct Small {
typedef _X X; // this is tedious!
X foo;
};
template<typename SomeSmall>
struct Big {
typedef typename SomeSmall::X X; // want to use X here!
SomeSmall bar;
X toe;
};
是否可以在不使用Small
类中的typedef的情况下访问Small
的模板参数X
?
Is there a way to access the template parameter X
of Small
without using a typedef in the Small
class?
推荐答案
根据您的工作,模板模板参数可能是一个更好的选择:
Depending on what you're doing, template template parameters might be a better option:
// "typename X" is a template type parameter. It accepts a type.
// "template <typename> class SomeSmall" is a template template parameter.
// It accepts a template that accepts a single type parameter.
template<typename X, template <typename> class SomeSmall>
struct Big {
SomeSmall<X> bar; // We pass X to the SomeSmall template.
X toe; // X is available to this template.
};
// Usage example:
template<typename X>
struct Small {
X foo;
};
struct MyType {};
// The foo member in Small will be of type MyType.
Big<MyType, Small> big;
这篇关于没有typedef的人可以访问模板外部的template参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文