为什么不能重载类模板? [英] Why is it not possible to overload class templates?

查看:150
本文介绍了为什么不能重载类模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读这个问题让我想到:是否有不允许类模板重载的技术原因?



通过重载,我的意思是有几个具有相同名称但不同参数的模板,例如

  template< typename T> 
struct Foo {};

template< typename T1,typename T2>
struct Foo {};

template< unsigned int N>
struct Foo {};

编译器管理处理重载的函数和函数模板,技术(例如名字捏造)到类模板?



首先,我认为当单独使用模板标识符时,可能会导致一些歧义问题,可以发生的是当它作为模板模板参数传递时,所以参数的类型可以用于选择适当的重载:

  template< template< typename>类T> 
void A {};

template< template< unsigned int>类T>
void B {};

A< Foo>一个; //解析为Foo< T>
B< Foo> b; //解析为Foo N

你认为这样的功能可能有用吗?是否有一些好(即技术)的原因,为什么这是不可能在当前C + +?

解决方案

第12.5节从完整指南模板 Amazon )包含此报价:


您可能会合法地想知道为什么只有类模板可以部分专门化。原因大多是历史。
可能可以为函数模板定义相同的机制(参见第13章)。



在某些方面,重载函数模板的
效果是类似的,但也有一些细微的差别。这些差异是
主要与当遇到使用时主要模板需要
查找的事实有关。专业化是
之后才考虑,以确定应该使用
的实现。



相比之下,所有重载的函数模板必须通过查找而将
带到重载集合中,并且它们可能来自
不同的命名空间或类。这增加了
无意中重载模板名称的可能性。



相反,
也可以想象为允许类模板重载的形式。
下面是一个示例:




  //类模板的无效重载
template< typename T1,typename T2>类对;
template< int N1,int N2>类对;




然而,似乎没有迫切的需要$此外, C ++的设计和演进 http://rads.stackoverflow.com/amzn/click/0201543303\">亚马逊)在第15.10.3节中包含此报价


因此我得出结论,我们需要一个专门化
模板的机制,这可以通过接受一般的重载
或一些更具体的机制来实现我选择了一个特定的机制
因为我认为我主要是解决由
不规则性引起的不规则性,并且因为建议的重载不变地
创建了一个反抗的尝试,我试图谨慎和
保守; I现在认为这是一个错误。专业化作为
最初定义是一种限制和异常形式的重载
与其余的语言适合。


< blockquote>

大胆强调我。我解释这说,函数重载分辨率比类专门化更难以实现(和用户获得)。所以可能没有真正的技术障碍(类似于功能模板部分专业化),但是一个历史意外。


Reading this question made me wonder: is there a technical reason for disallowing class templates overloads?

By overloading, I mean having several templates with the same names, but different parameters, for instance

template <typename T>
struct Foo {};

template <typename T1, typename T2>
struct Foo {};

template <unsigned int N>
struct Foo {};

The compiler manages to handle overloaded functions and function templates, wouldn't it be possible to apply the same techniques (e.g. name mangling) to class templates?

At first, I thought that perhaps that would cause some ambiguity issues when taking the template identifier alone, but the only time this can happen is when passing it as a template template argument, so the type of the parameter could be used to choose the appropriate overload:

template <template <typename> class T>
void A {};

template <template <unsigned int> class T>
void B {};

A<Foo> a; // resolves to Foo<T>
B<Foo> b; // resolves to Foo<N>

Do you think such feature could be useful? Is there some "good" (i.e. technical) reasons why this is not possible in current C++?

解决方案

Section 12.5 from Templates the Complete Guide (Amazon) contains this quote:

You may legitimately wonder why only class templates can be partially specialized. The reasons are mostly historical. It is probably possible to define the same mechanism for function templates (see Chapter 13).

In some ways the effect of overloading function templates is similar, but there are also some subtle differences. These differences are mostly related to the fact that the primary template needs to be looked up when a use is encountered. The specializations are considered only afterward, to determine which implementation should be used.

In contrast, all overloaded function templates must be brought into an overload set by looking them up, and they may come from different namespaces or classes. This increases the likelihood of unintentionally overloading a template name somewhat.

Conversely, it is also imaginable to allow a form of overloading of class templates. Here is an example:

// invalid overloading of class templates
template<typename T1, typename T2> class Pair; 
template<int N1, int N2> class Pair; 

However, there doesn't seem to be a pressing need for such a mechanism.

Furthermore, the Design and Evolution of C++ (Amazon) contains this quote in section 15.10.3

I therefore concluded that we needed a mechanism for "specializing templates. This could be done either by accepting general overloading or by some more specific mechanism. I chose a specific mechanism because I thought I was primarily addressing irregularities caused by irregularities in C and because suggestions of overloading invariably creates a howl of protests. I was trying to be cautious and conservative; I now consider that a mistake. Specialization as originally defined was a restricted and anomalous form of overloading that fitted poorly with the rest of the language.

Bold emphasis mine. I interpret this as saying that function overload resolution is more difficult to implement (and get right by users) than class specialization. So probably no real technical obstacles (similary for function template partial specialization) but an historical accident.

这篇关于为什么不能重载类模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆