本文介绍了是否可以创建模板别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 请考虑以下代码: template<模板typename ...>类... Ts> struct unite { template< typename ... T> struct type :Ts< T ...> ... {}; }; //这不工作::类型不命名一个类型,而是一个模板: // template<模板typename ...>类... Ts> // using unite_t = typename unite< Ts ...> :: type; template< typename> struct debug_none {} template< typename> struct debug_cout {}; template< typename ...> struct raise_demangled {}; template< typename ...> struct raise_specialized {}; template< type,typename = int> struct match_default {}; template<模板typename ...> class Control> void f() {} int main() { f //有什么方法来创建像unite_t这样的工作: // f< unite_t } 活动示例 问题:是否有任何方法来创建某种类型的模板别名类似于类型别名?(参见上面示例中的 unite_t )解决方案不,你不能。 使用一个变量。它不能返回模板。在其他地方没有类似的机制。 你可以通过约定所有的模板不是模板,而是一个模板<?>在其中使用apply = ?; 别名(当我们在它的时候,常量是 std :: integral_constants< T,?> ,并且指针 pointer_constant )。 。 模板只是种类( :: apply<?...> 。 将一组类型应用于此类模板将通过以下方式完成: template< class Z,class ... Ts> 使用apply_t = Z :: template apply< Ts ...> ;; / pre> 因此,使用原生模板 Z ,您可以 Z ; Ts ...> 。使用这些间接模板,您可以 apply_t< Z,Ts ...> 。 在这个约定下,使用别名的模板可以返回一个间接模板。约定总是调用 apply_t 来应用模板,并且您间接地使用您编写的所有其他模板。我们完成了。 这很丑陋。 Consider the following code:template< template< typename ... > class ... Ts >struct unite{ template< typename ... T > struct type : Ts< T ... > ... { };};// This does not work as ::type does not name a type, but a template:// template< template< typename ... > class ... Ts >// using unite_t = typename unite< Ts ... >::type;template< typename > struct debug_none {};template< typename > struct debug_cout {};template< typename ... > struct raise_demangled {};template< typename ... > struct raise_specialized {};template< typename, typename = int > struct match_default {};template< template< typename ... > class Control >void f(){}int main(){ f< unite< debug_none, raise_demangled, match_default >::type >(); // Is there any way to create something like unite_t which works like this: // f< unite_t< debug_none, raise_demangled, match_default > >();}Live exampleQuestion: Is there any way to create some kind of "template alias" similar to a type alias? (see unite_t in the above example) 解决方案 No, you cannot.using can "return" a type, or a variable. It cannot "return" a template. There are no similar mechanisms elsewhere.You can do something vaguely useful by taking the convention that all templates are not templates, but rather classes with a template<?>using apply=?; alias inside them (and while we are at it, constants are std::integral_constants<T,?>, and pointers are pointer_constant<T*,?>).Now everything is a class. templates become just kinds of classes (with a ::apply<?...>.Applying a bundle of types to such a template would be done via:template<class Z, class...Ts>using apply_t = Z::template apply<Ts...>;So with a "native" template Z, you'd do Z<Ts...>. With these "indirect" templates, you'd do apply_t<Z, Ts...>.With this convention, a template using alias can return an indirect template. If the rest of your code follows the convention of always calling apply_t to apply a template, and you indirect-ize all other templates you write, we are done.This is ugly. 这篇关于是否可以创建模板别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 11:18