在Dev-c++中,函数模板不支持偏特化?

xlsue 2005-10-30 11:43:17
A natural syntax for partially specializing function templates is the generalization of the class template notation:

template <typename T>
T const& max (T const&, T const&); // primary template

template <typename T>
T* const& max <T*>(T* const&, T* const&); // partial specialization
-------------------------------------------------------------------------------------------------------------------
这在Dev-c++中提示:
8 G:\Dev-Cpp\函数template特化与重载\main.cpp partial specialization `max<T*>' of function template
...全文
439 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ifeelhappy 2005-11-11
  • 打赏
  • 举报
回复
为什么我的类也不支持偏特化呢?
Jinhao 2005-10-30
  • 打赏
  • 举报
回复
支持刀刀~~
函数模板没有偏特化一说~~~至少目前没有
xlsue 2005-10-30
  • 打赏
  • 举报
回复
回复人: BluntBlade(无锋之刃·回炉再造) ( ) 信誉:105 2005-10-30 12:06:00 得分: 0


当前C++标准不支持函数模板的特化。
----------------------------------------------------------------------------------------------------------------
这个观点我比较同意.

BluntBlade 2005-10-30
  • 打赏
  • 举报
回复
当前C++标准不支持函数模板的特化。
fiftymetre 2005-10-30
  • 打赏
  • 举报
回复
用VC
xlsue 2005-10-30
  • 打赏
  • 举报
回复
贴上完整的给他们看:
13.7 Partial Specialization of Function Templates
In Chapter 12 we discussed how class templates can be partially specialized, whereas function templates are simply overloaded. The two mechanisms are somewhat different.

Partial specialization doesn't introduce a completely new template: It is an extension of an existing template (the primary template). When a class template is looked up, only primary templates are considered at first. If, after the selection of a primary template, it turns out that there is a partial specialization of that template with a template argument pattern that matches that of the instantiation, its definition (in other words, its body) is instantiated instead of the definition of the primary template. (Full template specializations work exactly the same way.)

In contrast, overloaded function templates are separate templates that are completely independent of one another. When selecting which template to instantiate, all the overloaded templates are considered together, and overload resolution attempts to choose one as the best fit. At first this might seem like an adequate alternative, but in practice there are a number of limitations:

It is possible to specialize member templates of a class without changing the definition of that class. However, adding an overloaded member does require a change in the definition of a class. In many cases this is not an option because we may not own the rights to do so. Furthermore, the C++ standard does not currently allow us to add new templates to the std namespace, but it does allow us to specialize templates from that namespace.

To overload function templates, their function parameters must differ in some material way. Consider a function template R convert(T const&) where R and T are template parameters. We may very well want to specialize this template for R = void, but this cannot be done using overloading.

Code that is valid for a nonoverloaded function may no longer be valid when the function is overloaded. Specifically, given two function templates f(T) and g(T) (where T is a template parameter), the expression g(&f<int>) is valid only if f is not overloaded (otherwise, there is no way to decide which f is meant).

Friend declarations refer to a specific function template or an instantiation of a specific function template. An overloaded version of a function template would not automatically have the privileges granted to the original template.

Together, this list forms a compelling argument in support of a partial specialization construct for function templates.

A natural syntax for partially specializing function templates is the generalization of the class template notation:

template <typename T>
T const& max (T const&, T const&); // primary template

template <typename T>
T* const& max <T*>(T* const&, T* const&); // partial specialization
Some language designers worry about the interaction of this partial specialization approach with function template overloading. For example:

template <typename T>
void add (T& x, int i); // a primary template

template <typename T1, typename T2>
void add (T1 a, T2 b); // another (overloaded) primary template

template <typename T>
void add<T*> (T*&, int); // which primary template does this specialize?
However, we expect such cases would be deemed errors without major impact on the utility of the feature.

At the time of this writing, this extension is under consideration by the C++ standardization committee.

utstar 2005-10-30
  • 打赏
  • 举报
回复
gz
foochow 2005-10-30
  • 打赏
  • 举报
回复
Effective C++ 3rd Edition中也提到了这一点...
foochow 2005-10-30
  • 打赏
  • 举报
回复
<<C++ templates>> 13.7 Partial Specialization of Function Templates

At the time of this writing, this extension is under consideration by the C++ standardization committee.
......................................

目前C++允许类模板的部分特化,但不允许函数模板这样做.这样的代码不符合标准,尽管一些编译器错误地接受了它..GCC4.0.0,VC7均不支持...
xlsue 2005-10-30
  • 打赏
  • 举报
回复
详见<<C++ templates>> 13.7 Partial Specialization of Function Templates
xlsue 2005-10-30
  • 打赏
  • 举报
回复
有是有,不过可能还没有加入到标准中,C++标准98版没发现提到这个东西.较新版的编译器应该实现出来了...
foochow 2005-10-30
  • 打赏
  • 举报
回复
只有类模板才能定义部分特殊化。。

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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