好奇地反复出现模板模式 [英] Curiously recurring template pattern
问题描述
关于奇怪的重复模板模式的一些问题:
此页:
http://c2.com/cgi/wiki?CuriouslyRecurringTemplate
这部分:
模板< typename T> struct ArithmeticType
{
T operator +(const T& other)const
{
T result(*这个); //< ---------此行
结果+ =其他;
返回结果;
}
//等等
};
在上面标记的行中,为什么一个人不需要任何类型的
从*演员到const T& ?
如果需要施法,你会使用什么类型的施法?
我认为reinterpret_cast不可移植(理论上,或者
也在实践中?)并且无论如何都不处理MI的情况,并且
dynamic_cast很慢并且仅适用于polimorphic类型,对吧?
本页的另一个问题:
http://www.informit.com/articles/art...31473&seqNum=3
最后有写道:
----
一般来说,CRTP对于分解接口的实现非常有用
只能是成员函数(例如,构造函数,
析构函数和下标运算符)。
----
对于下标我可以也许可以理解,但你怎么能为
构造函数和析构函数做?
谢谢
iuweriu r
A few questions on the curiously recurring template pattern:
This page:
http://c2.com/cgi/wiki?CuriouslyRecurringTemplate
this part:
template<typename T> struct ArithmeticType
{
T operator + (const T& other) const
{
T result(*this); // <--------- THIS LINE
result += other;
return result;
}
// etc.
};
In the above marked line, how comes that one doesn''t need any type of
cast from *this to const T& ?
And if the cast is needed, what type of cast would you use?
I suppose that the reinterpret_cast would not be portable (in theory, or
also in practice?) and anyway does not handle cases with MI, and the
dynamic_cast is slow and works only on polimorphic types, right?
Another question on this page:
http://www.informit.com/articles/art...31473&seqNum=3
At the end there is written:
----
In general, CRTP is useful to factor out implementations of interfaces
that can only be member functions (for example, constructor,
destructors, and subscript operators).
----
For the subscript I can maybe understand, but how can you do it for
constructors and destructors?
Thank you
iuweriur
推荐答案
* iuweriur:
* iuweriur:
关于奇怪的重复模板模式的几个问题:
此页:
http://c2.com/ cgi / wiki?CuriouslyRecurringTemplate
这部分:
模板< typename T> struct ArithmeticType
{运算符+(const T& other)const
{结果(* this); //< ---------此行
结果+ =其他;
返回结果;
}
//等
};
在上面标记的行中,为什么一个人不需要任何类型的演员从* this到const T& ?
据推测,目标是类T有一个公共构造函数,它使得算术类型< T>参数并将dynamic_cast用于T.
我认为这很难看,而且在ArithmeticType中做一个贬低也很难看。
向下转型是虚拟功能所有以类型 -
安全有效的方式实现的,所以我会写
模板< typename T>
class ArithmeticType
{
protected:
虚拟T const& thisT()const = 0;
T运算符+(const T& other)const
{
T结果(thisT ());
结果+ =其他;
返回结果;
}
//等
};
如果需要施法,你会使用什么类型的施法?
无需演员表;见上文。
本页的另一个问题:
http://www.informit.com/articles/art...31473&seqNum=3
最后有写道:
----
一般来说,CRTP可以分解出只能是成员函数的接口实现(例如,构造函数,
析构函数)和下标操作符。
----
对于下标我可以理解,但你怎么能为
构造函数和析构函数做?
A few questions on the curiously recurring template pattern:
This page:
http://c2.com/cgi/wiki?CuriouslyRecurringTemplate
this part:
template<typename T> struct ArithmeticType
{
T operator + (const T& other) const
{
T result(*this); // <--------- THIS LINE
result += other;
return result;
}
// etc.
};
In the above marked line, how comes that one doesn''t need any type of
cast from *this to const T& ?
Presumably the intent is that class T has a public constructor that takes
an ArithmeticType<T> argument and uses dynamic_cast down to T.
I think that''s ugly, and doing a downcast in ArithmeticType is also ugly.
Downcasting is what virtual functions are all about achieving in a type-
safe and efficient manner, so I''d write
template<typename T>
class ArithmeticType
{
protected:
virtual T const& thisT() const = 0;
T operator + (const T& other) const
{
T result( thisT() );
result += other;
return result;
}
// etc.
};
And if the cast is needed, what type of cast would you use?
No cast needed; see above.
Another question on this page:
http://www.informit.com/articles/art...31473&seqNum=3
At the end there is written:
----
In general, CRTP is useful to factor out implementations of interfaces
that can only be member functions (for example, constructor,
destructors, and subscript operators).
----
For the subscript I can maybe understand, but how can you do it for
constructors and destructors?
一组类可能有非常相似的构造函数或构造函数部分,
仅在所涉及的具体类型上有所不同。
这样的构造函数可以在CRT基类中计算出来。
例如,CRT基类可能是Window,而典型派生的
class Button和Button可能有一个静态函数n ativeWindowHandle()
,Window类构造函数调用;请参阅常见问题解答23.4了解其他技术
在不使用CRT模式的情况下大致相同。
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门帖子。
问:usenet和电子邮件中最烦人的事情是什么?
A set of classes may have very similar constructors or constructor parts,
differing only in the concrete types involved.
Such a constructor can be factored out in a CRT base class.
For example, the CRT base class might be Window, and a typical derived
class Button, and Button might have a static function nativeWindowHandle()
that the Window class constructor calls; see FAQ 23.4 for other techniques
to do just about the same without using the CRT pattern.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
" Alf P. Steinbach" <人*** @ start.no>在消息中写道
news:40 ************** @ news.individual.net ...
"Alf P. Steinbach" <al***@start.no> wrote in message
news:40**************@news.individual.net...
* iuweriur:
* iuweriur:
关于奇怪的重复模板模式的一些问题:
此页面:
http://c2.com/cgi/wiki?CuriouslyRecurringTemplate
此部分:
template< typename T> struct ArithmeticType
{运算符+(const T& other)const
{结果(* this); //< ---------此行
结果+ =其他;
返回结果;
}
//等
};
在上面标记的行中,为什么一个人不需要任何类型
的演员从* this到const T& ?
大概是意图是类T有一个公共构造函数
A few questions on the curiously recurring template pattern:
This page:
http://c2.com/cgi/wiki?CuriouslyRecurringTemplate
this part:
template<typename T> struct ArithmeticType
{
T operator + (const T& other) const
{
T result(*this); // <--------- THIS LINE
result += other;
return result;
}
// etc.
};
In the above marked line, how comes that one doesn''t need any type of cast from *this to const T& ?
Presumably the intent is that class T has a public constructor that
采用ArithmeticType< T>参数并使用dynamic_cast直到T。
takes an ArithmeticType<T> argument and uses dynamic_cast down to T.
static_cast应该在这里做。我认为这是上面例子中
所要求的。也许作者使用的是VC6。
Jonathan
A static_cast should do the trick here. I think it''s required in the
above example. Maybe the authors were using VC6.
Jonathan
" Jonathan Turkanis" < TE ****** @ kangaroologic.com>在消息中写道
news:2l ************ @ uni-berlin.de ...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:2l************@uni-berlin.de...
static_cast应该在这里做的伎俩。我认为这是上面例子中的要求。也许作者使用VC6。
A static_cast should do the trick here. I think it''s required in the
above example. Maybe the authors were using VC6.
哎呀。 VC6也需要演员。
Whoops. VC6 needs the cast too.
这篇关于好奇地反复出现模板模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!