C++ 中的显式专业化 [英] Explicit Specialization in C++

查看:32
本文介绍了C++ 中的显式专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Primer C++ > Adventures in Functions > Templates > Explicit Specialization.

I'm reading Primer C++ > Adventures in Functions > Templates > Explicit Specialization.

为了说明显式专业化的原因/用途,举例说明.考虑一个可以交换任何类型(int、double、struct 等)的交换模板函数(我认为代码一目了然所以不用写在这里)

To show the reason/use for Explicit Specialization, a case is illustrated. Consider a swap template function that can swap any type (int, double, struct, etc...) (I think the code is obvious so no need to write it here)

但是有一个特定的结构(称为作业),您只想交换其中的两个成员,而其他成员保持原样.您将需要不同的定义,因此您必须进行显式专业化.

But there is a specific struct (called job) that you want to only swap two members of, and leave the rest of the members as they are. You will need a different definition, and so you will have to make an Explicit Specialization.

在同一节中有这样的声明:-"特化覆盖常规模板,非模板函数覆盖两个都."为什么不为该用途制作一个(常规)函数?那么常规/非模板会覆盖模板吗?

There is this statement in the same section:- "A specialization overrides the regular template, and a non-template function overrides both." Why not just make a (regular) function for that use? Then a regular/non-template will override the template?

如果我的解决方案是正确的,那么什么是显式专业化的好例子?

If my solution is correct, then what is a good example for Explicit Specialization?

推荐答案

显式特化的用例之一是避免在实际 template 函数中发生某些更改时跳过常规函数.要理解,请参见以下示例:

One of the use case for explicit specialization is to avoid regular function to be skipped when some changes happens in the actual template function. To understand see below example:

template<typename T1, typename T2>
void foo(T1 o1, T2 o2)  // template function
{}
void foo(int o1, int o2) // regular function overloaded
{}

到现在都还好.现在一段时间后,您必须更改 template<> 的定义.foo()

Till now it's fine. Now after sometime you got to change the definition of template<> foo()

template<typename T1, typename T2, typename T3> // new parameter added
void foo(T1 o1, T2 o2, T3 o3)  // template function
{}

您相应地更改了对 foo() 的所有调用,但您错过/搞砸了以更改常规重载函数 foo().然后,这是一场灾难!因为编译会顺利进行,并且常规调用将悄悄地替换为模板<>foo(),这是不受欢迎的.

You changed all the calls to foo() accordingly, but you missed/messed to change the regular overloaded function foo(). Then, it's a disaster ! Because compilation would go fine and regular calls would be silently replaced by the template<> foo(), which is undesired.

现在,如果有一个明确的专业化,例如,

Now, had there been an explicit specialization such as,

template<>
void foo(int o1, int o2) // explicit specialization
{}

那么该函数会因参数不匹配而导致编译错误,并提醒您进行相应的更改.

then that function will give you compilation error due to unmatched parameters and which would remind you of corresponding changes.

另一种用法 or(区别)是可以在头文件中包含一个明确的专用函数,而不用担心多符号链接错误.请注意,显式特化也有其自身的缺点,但我有演示它的优点.

The other usage or (difference) is that an explicitly specialized function can be included in header file without any concern of multiple symbol linking error. Note that, explicit specialization has its own drabacks also, but I have demo a good side of it.

这篇关于C++ 中的显式专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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