【trait和policy之区别和联系,以及应用。】大家畅所欲言吧~~~~~

adamsun 2004-04-02 10:54:03
我在boost的mailing-list中看到他们在讨论这个,故发帖听听大家的高见。

:-)

//---------------------------------------------------------------
下面是从boost members中的Rob Stewart的邮件中摘过来的

Trait - A class template parameterized on a single type that
associates information with that type. A traits class,
therefore, provides an external, named grouping of
metainformation, behavior, or both for that type. A trait class
is never passed as a template parameter; it's name is ubiquitous.
A traits class never has state.

Policy - A class template passed to other templates for the
purpose of providing a named grouping of metainformation,
behavior, or both to those templates. Because it is a template
parameter, different policy classes can be used as desired. In
many cases, a policy class is used as a base class.

//----------------------------------------------------------------------
下面是从boost members中的Mark Rodgers的完整邮件

Well I think in terms of code, and it really is quite simple IMHO:

Traits look like this:

template< typename T >
class SomeTraits
{
// Stuff that tells us about T
};

and Policies look like this

template< typename Policy >
class SomeThing
{
// Delegates some behaviour to Policy
};

IOW, traits are class templates and policies are template parameters so
there really can't be any confusion between the two. So in the case
std::basic_string

template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_string {

"char_traits" is a traits class template but "traits" is actually a policy
template parameter. It is quite reasonable for specialisations of traits
templates to be used as arguments to policy parameters, which is exactly
what happens when we write

std::basic_string< char,std::char_traits<char> >

But I don't think there is anything in basic_string that requires the
template argument to be a specialisation of a class template. I think it
would be quite legitimate to write

class MyFunkyCharBehaviour { /*...*/ };
typedef std::basic_string<char,MyFunkyCharBehaviour> MyFunkyString;

so the "traits" parameter is indeed misnamed.

However std::char_traits is most definitely a traits template and doesn't
*have* to be used as a policy. In fact std::basic_string could have been
written to exclusively use char_traits *instead* of a policy.

Mark

//----------------------------------------------------------------------
...全文
295 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
adamsun 2004-04-15
  • 打赏
  • 举报
回复
结帖吧,虽有宏愿,无力回天~~~~~~~~~~~

:-(
adamsun 2004-04-09
  • 打赏
  • 举报
回复
To:abitz(阿奈)
我一直想彻底搞搞boost原代码,无奈时间和精力有限,无法如愿。无论是为了疯狂还是技术,

能做自己喜欢的事,现在想来是种奢侈!sign............
BuW 2004-04-09
  • 打赏
  • 举报
回复
偶也是菜鸟。
学习一下!~~~~~~
abitz 2004-04-09
  • 打赏
  • 举报
回复
最近编程中对policy的静态多态作用有点体会。
你不妨试着写一些同样功能体系,分别用动态多态与静态多态实现,
看看他们的能力的差别与各自的有缺点。

关于boost专题:最近比较忙,加之功力又不够,写东西比较困难。
但过一阵应该会有人写一些东西的。
大家看到什么好的文章请告诉我们一声。
关于boost最直接的文章就是boost的文档,看看文档还是很有好处的。
mailing list里的东西比较杂,但有很多有价值材料。

》》最近我们三个学生执掌这里之后,很多高手都消失了【哭】
sigh......

andy_show 2004-04-06
  • 打赏
  • 举报
回复
其实traits和policy并无本质上的区别,一种简单的理解方式就是这些东西都是编译时确定的抽象层,从某种意义上来说,其实和C++的抽象类的作用有点类似,只不过C++里边的抽象类是运行时动态绑定,而traits和policy在编译时确定,根据<<Modern C++ Design>>中的说法,traits和policy的区别好像仅在于traits着重于提供对象的属性,而policy则着重于提供对象的行为。
其实不难发现,所谓GP很多东西和C++里的传统的设计方法并无本质的区别,GP把很多传统的c++里多态的设计方法应用到了模板里边,就比如trait和policy其实和C++里的抽象类的作用非常类似。

例如
traits或者policy都定义了一组具有共性的操作或者属性,C++的抽象基类也是
traits或者policy的使用者通过traits或者policy的提供的接口提供的操作屏蔽对具体类型的依赖,实现代码重用。C++的抽象类的使用者也是如此。只不过在具体的使用上有所不同,比如traits和policy是编译时确定,而C++的抽象类是运行时确定,traits和policy的实现通过模板参数传递给使用者,而C++的抽象类的实现通过指针传递给使用者。
只要理解这一点,不管以后出现什么behavior, factory, strategy这样的东西,都不会感到惊奇。

adamsun 2004-04-06
  • 打赏
  • 举报
回复
好~
adamsun 2004-04-05
  • 打赏
  • 举报
回复
上面的是<<C++ Template>>一书中的,已经归类的很好了,谢谢lifanxi(Byron) 。
lifanxi 2004-04-04
  • 打赏
  • 举报
回复
15.1.5 Traits and Policies: What's the Difference?
A reasonable case can be made in support of the fact that policies are just a special case of traits. Conversely, it could be claimed that traits just encode a policy.

The New Shorter Oxford English Dictionary (see [NewShorterOED]) has this to say:

trait n. ... a distinctive feature characterizing a thing

policy n. ... any course of action adopted as advantegous or expedient

Based on this, we tend to limit the use of the term policy classes to classes that encode an action of some sort that is largely orthogonal with respect to any other template argument with which it is combined. This is in agreement with Andrei Alexandrescu's statement in his book Modern C++ Design (see page 8 of [AlexandrescuDesign]) [6]:

[6] Alexandrescu has been the main voice in the world of policy classes, and he has developed a rich set of techniques based on them.

Policies have much in common with traits but differ in that they put less emphasis on type and more on behavior.

Nathan Myers, who introduced the traits technique, proposed the following more open-ended definition (see [MyersTraits]):

Traits class: A class used in place of template parameters. As a class, it aggregates useful types and constants; as a template, it provides an avenue for that "extra level of indirection" that solves all software problems.

In general, we therefore tend to use the following (slightly fuzzy) definitions:

Traits represent natural additional properties of a template parameter.

Policies represent configurable behavior for generic functions and types (often with some commonly used defaults).

To elaborate further on the possible distinctions between the two concepts, we list the following observations about traits:

Traits can be useful as fixed traits (that is, without being passed through template parameters).

Traits parameters usually have very natural default values (which are rarely overridden, or simply cannot be overridden).

Traits parameters tend to depend tightly on one or more main parameters.

Traits mostly combine types and constants rather than member functions.

Traits tend to be collected in traits templates.

For policy classes, we make the following observations:

Policy classes don't contribute much if they aren't passed as template parameters.

Policy parameters need not have default values and are often specified explicitly (although many generic components are configured with commonly used default policies).

Policy parameters are mostly orthogonal to other parameters of a template.

Policy classes mostly combine member functions.

Policies can be collected in plain classes or in class templates.

However, there is certainly an indistinct line between both terms. For example, the character traits of the C++ standard library also define functional behavior such as comparing, moving, and finding characters. And by replacing these traits you can define string classes that behave in a case-insensitive manner (see Section 11.2.14 in [JosuttisStdLib]) while keeping the same character type. Thus, although they are called traits, they have some properties associated with policies.

adamsun 2004-04-04
  • 打赏
  • 举报
回复
谢谢~~~
lifanxi 2004-04-04
  • 打赏
  • 举报
回复
有影印版,china-pub正在捆绑销售。
我看的是电子版打印的。
我的英文名字是初中英语老师起的,可惜我只知道Byron,却不喜欢读诗~~~
adamsun 2004-04-04
  • 打赏
  • 举报
回复
To:lifanxi(Byron)

<<C++ Templates>>一书我也在读,中文版,国内是不是有英文影印版?

我在网上书店看到有这么一则消息。

adamsun 2004-04-04
  • 打赏
  • 举报
回复
To:lifanxi(Byron)

谢谢拉~~~ :-)

Byron(拜伦)的诗估计你也看过不少吧~~~
lifanxi 2004-04-04
  • 打赏
  • 举报
回复
最近看C++ Templates一书,个人上面对这个问题解释的还是不错的,一会儿我把相关的内容提供给大家参考。
adamsun 2004-04-04
  • 打赏
  • 举报
回复
楼上二位的观点让我受教了,谢谢!
zhuk_nir 2004-04-04
  • 打赏
  • 举报
回复
我觉得traits有点像静态的RTTI,而policy把一个类切分成几个正交的概念,通过policy的组合得到想要的功能。
adamsun 2004-04-03
  • 打赏
  • 举报
回复
这两天boost的mailing-list里对这个讨论的很多,Wolf0403(完美废人)(sf.net,我的乐土)

兄你看看谁能给讲讲啊?我坐板凳洗耳恭听。。。

:-)
adamsun 2004-04-03
  • 打赏
  • 举报
回复
To: Wolf0403(完美废人)(sf.net,我的乐土)

你在这里很热心,而且技术也很让我欣赏,你说的boost专题什么时候出啊?我望眼欲穿啊。

我只是抛砖引玉,期待能人说话~~~~~~~

:-)
adamsun 2004-04-03
  • 打赏
  • 举报
回复
我在网吧上网,恕我无法翻译它。我本身对Trait和Policy的理解也有限,想到这里来

请大家说说它们。另外我关注Boost纯粹出于个人兴趣,我的C++还远没有达到如此高度。

用时下流行的话说--是“菜鸟”。

:-)
Wolf0403 2004-04-03
  • 打赏
  • 举报
回复
Rob Stewart 的看法是。。。Policy 是一个类本身的属性,而 traits 是外加的一组信息。
我猜想可能 policy 是通过模板类、参数类和 traits 共同表现出来的约束罢了;traits 是更具像的一种东西
b_horse 2004-04-03
  • 打赏
  • 举报
回复
从概念上讲,traits是基于类型的属性,policy是基于类型的方法或称其为方案更贴切。
从设计角度上讲,policy是一种程序设计方法,它规定的是一套程序运行的“标准接口”,有点想“编译期的多态”用法,利用的是模板和多继承;
而trait想告诉我们的是一种更贴近于具体类型的东西,你可以使用偏特化的方法让它“七十二变”,呵呵,感觉更像一种“面向类型”的编程概念。
两者偏重的角度是不同的,policy多用于骨架的搭建,trait更对的是对血肉的填充。
加载更多回复(4)
在Windows 10或Windows 11操作系统中,用户经常会遇到共享打印机时出现的一系列错误代码,这些错误代码可能会阻碍打印机共享功能的正常使用。常见的错误代码包括0x00000057、0x00000709和0x0000011b,这些代码通常指出了不同的问题,比如权限不足、服务未运行或配置错误等。除此之外,还有一些故障提示如“连接失败”或“内存不足”,这些都可能影响到打印机共享的稳定性。 要解决这些故障,首先要确保打印机已经正确地连接到网络,并且在需要共享的电脑上进行了设置。确保打印机驱动程序是最新的,并且在共享设置中没有错误配置。对于权限问题,需要检查网络上的用户账户是否具有足够的权限来访问共享打印机。同时,也要确保打印机服务正在运行,特别是“Print Spooler”服务,因为这是打印机共享服务的核心组件。 在某些情况下,问题可能与操作系统的更新有关,如升级到最新版的Windows 10或Windows 11后可能出现的兼容性问题。这时,可能需要查看微软的官方支持文档来获取特定的解决方案或更新。 对于错误代码0x00000057,这通常是由于没有足够的权限来访问网络打印机或其共享资源,解决方法是确保网络打印机的权限设置正确,包括在组策略中设置相应的访问权限。而0x00000709错误可能是由于打印机驱动问题或打印机端口配置错误,可以尝试重新安装或更新打印机驱动来解决。至于0x0000011b错误,这往往是因为打印机队列服务的问题,检查并重启“Print Spooler”服务通常是解决这类问题的常见手段。 至于“连接失败”或“内存不足”这类故障,通常与客户端和打印机之间的网络连接以及打印机本地资源的使用情况有关。检查网络连接,确保打印机所在的网络段没有故障或中断。同时,如果打印机的打印队列长时间得不到处理,可能会导致内存不足的情况,这时可能需要清理打印队列或增加打印机的内存配置。 为了帮助用户更快速地解决这些问题,市面上出现了各种打印机共享错误修复工具。这些工具往往通过预设的修复程序来自动检测和修正打印机共享中常见的问题。它们可以快速检查打印机驱动、网络连接以及共享设置,并且能够提供一键修复功能,大幅减少了用户自行排查和解决问题的难度。 然而,在使用这些修复工具之前,用户应确保这些工具的来源是安全可靠的,避免因使用不当的修复工具而引发其他系统安全或隐私问题。用户可以到官方平台或者信誉良好的软件提供商处下载这些工具。通过细心检查打印机的共享设置,及时更新驱动程序和服务,以及合理使用修复工具,大多数共享打印机的问题都可以得到有效的解决。

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧