模板专业化VS功能重载 [英] Template Specialization VS Function Overloading

查看:88
本文介绍了模板专业化VS功能重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

教科书我注意到,您可以通过模板专门化为函数重载提供您自己的标准库函数的实现,例如 swap(x,y)。这对于可以受益于除了赋值交换之外的其他类型的任何类型是有用的,例如 STL容器(已经写入了交换,我知道)。

A textbook I have notes that you can provide your own implementation for standard library functions like swap(x,y) via template specialization for function overloading. This would be useful for any types which can benefit from something other than an assignment swap, like STL containers for example (which already have swaps written, I know).

我的问题是:


  1. :模板专门化给你专门的
    交换实现,或者函数重载提供你想要使用没有模板的确切的
    参数。

  1. What's better: template specialization to give your specialized swap implementation, or function overloading providing the exact parameters you wish to use without a template?

为什么会更好?


推荐答案

:重载,当你可以,专门在你需要时。

Short story: overload when you can, specialise when you need to.

长故事:C ++对待专业化和重载非常不同。这最好用一个例子解释。

Long story: C++ treats specialisation and overloads very differently. This is best explained with an example.

template <typename T> void foo(T);
template <typename T> void foo(T*); // overload of foo(T)
template <>           void foo<int>(int*); // specialisation of foo(T*)

foo(new int); // calls foo<int>(int*);

现在让我们换掉最后两个。

Now let's swap the last two.

template <typename T> void foo(T);
template <>           void foo<int*>(int*); // specialisation of foo(T)
template <typename T> void foo(T*); // overload of foo(T)

foo(new int); // calls foo(T*) !!!

编译器在甚至查看特殊化之前会重载解析。因此,在这两种情况下,重载解析选择 foo(T *)。然而,只有在第一种情况下,它找到 foo (int *),因为在第二种情况下 int * specialization是 foo(T)的专业化,而不是 foo(T *)

The compiler does overload resolution before it even looks at specialisations. So, in both cases, overload resolution chooses foo(T*). However, only in the first case does it find foo<int*>(int*) because in the second case the int* specialisation is a specialisation of foo(T), not foo(T*).

您提到了 std :: swap 。这使得事情变得更加复杂。

You mentioned std::swap. This makes things even more complicated.

标准说,您可以向 std 命名空间添加专门化。太棒了,所以你有一些 Foo 类型,它有一个性能交换,那么你只是专门交换(Foo& amp; Foo&) std 命名空间。没有问题。

The standard says that you can add specialisations to the std namespace. Great, so you have some Foo type and it has a performant swap then you just specialise swap(Foo&, Foo&) in the std namespace. No problems.

但是如果 Foo 是一个模板类呢? C ++没有函数的部分专门化,所以你不能专门化 swap 。你的唯一选择是重载,但是标准规定不允许在 std 命名空间中添加重载!

But what if Foo is a template class? C++ doesn't have partial specialisation of functions, so you can't specialise swap. Your only choice is overloading, but the standard says that you aren't allowed to add overloads into the std namespace!

此时您有两个选项:


  1. 创建交换(Foo< T& ; Foo T&)函数在你自己的命名空间,并希望它通过ADL找到。我说希望,因为如果标准库调用交换像 std :: swap(a,b); 那么ADL根本不工作。

  1. Create a swap(Foo<T>&, Foo<T>&) function in your own namespace, and hope that it gets found via ADL. I say "hope" because if the standard library calls swap like std::swap(a, b); then ADL simply won't work.

忽略标准中不要添加重载的部分,无论如何。老实说,即使它在技术上不允许,在所有现实的情况下它都可以工作。

Ignore the part of the standard that says not to add overloads and do it anyway. Honestly, even though it's technically not allowed, in all realistic scenarios it's going to work.

是不能保证标准库使用 swap 。大多数算法使用 std :: iter_swap ,在我看过的一些实现中,它并不总是转发到 std :: swap

One thing to remember though is that there's no guarantee that the standard library uses swap at all. Most algorithms use std::iter_swap and in some implementations that I've looked at, it doesn't always forward to std::swap.

这篇关于模板专业化VS功能重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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