专用模板继承自非专用版本

专用模板继承自非专用版本

本文介绍了C ++专用模板继承自非专用版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图解决一个问题,但发现了一个不同的解决方案。 但是出于好奇,想知道以下是否可能: template< class> struct S; template< > struct S 我想能够从专门的结构继承非专有结构。上面的例子不工作,因为继承的结构是专门的,导致无限递归。 一个可能的解决方案是添加第二个模板参数,说bool专门的,使得默认值为false,专用模板有那个参数是true.however,使事情有点凌乱,因为实例化需要指定额外的参数。 有其他方法来实现上述? / p> 最初的问题是实现矩阵矩阵,其中矩阵本身可能有额外的运算符,这取决于组成的矩阵有那些运算符。我希望这是有意义的。同时不同的专用矩阵需要具有相同的基类,同时保留相同的名称,尽管具有不同的模板参数。我认为可能有一种方法使用enable_if和类型traits做解决方案你可以保留所有的通用东西在一个单独类型,并扩展您的专业化: 模板< typename> struct S_generic {/ * generic stuff here * /}; template< typename T> struct S:public S_generic< T> { /* 这里没有什么 */ }; 模板<> struct S< Foo> :public S_generic< Foo> {/ * extra stuff here * /}; 编辑:如果您不喜欢额外的名称,在实例化模板时使用额外标志而没有混乱的方式是使用默认值: template< typename T, bool fully_defined = true> struct S; template< typename T> struct S< T,false> {/ * generic stuff here * /}; template< typename T> struct S 模板<> struct S I was trying to solve a problem, but found a different solution.however out of curiosity like to know if the following is possible:template< class > struct S;template< > struct S< Foo > : struct< Foo > {};I would like to be able to inherit nonspecialized struct from specialized struct.the example above does not work because the inherited struct is the specialized, leading to the infinite recursion.One possible solution was to add second template parameter, say bool specialized, such that the default is false, and specialized template has that parameter is true.however that makes things a bit messy since instantiation needs to specify additional parameter.Is there some other way around to implement the above?the original problem was to implement matrix of matrixes, where matrix itself may have additional operators, depending if the constituent matrixes has those operators.I would hope that makes sense. at the same time different specialized matrix need to be of the same base class at the same time retaining the same name, although with different template parameters. I have thought there might be a way to do it using enable_if and type traits 解决方案 You could keep all the generic stuff in a separate type, and extend that with your specialisation:template <typename> struct S_generic { /* generic stuff here */ };template <typename T> struct S : public S_generic<T> { /* nothing here */ };template <> struct S<Foo> : public S_generic<Foo> { /* extra stuff here */ };Edit: Alternatively, if you don't like the extra name, the way to use an extra flag without messiness when instantiating the template is to use a default value:template <typename T, bool fully_defined=true> struct S;template <typename T> struct S<T,false> { /* generic stuff here */ };template <typename T> struct S<T,true> : public S<T,false> {};template <> struct S<Foo,true> : public S<Foo,false> { /* extra stuff here */ }; 这篇关于C ++专用模板继承自非专用版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 01:09