<legend id='KoXxj'><style id='KoXxj'><dir id='KoXxj'><q id='KoXxj'></q></dir></style></legend>

    <small id='KoXxj'></small><noframes id='KoXxj'>

      <bdo id='KoXxj'></bdo><ul id='KoXxj'></ul>

    <tfoot id='KoXxj'></tfoot>
      <i id='KoXxj'><tr id='KoXxj'><dt id='KoXxj'><q id='KoXxj'><span id='KoXxj'><b id='KoXxj'><form id='KoXxj'><ins id='KoXxj'></ins><ul id='KoXxj'></ul><sub id='KoXxj'></sub></form><legend id='KoXxj'></legend><bdo id='KoXxj'><pre id='KoXxj'><center id='KoXxj'></center></pre></bdo></b><th id='KoXxj'></th></span></q></dt></tr></i><div id='KoXxj'><tfoot id='KoXxj'></tfoot><dl id='KoXxj'><fieldset id='KoXxj'></fieldset></dl></div>
    1. 模板专业化 VS 函数重载

      Template Specialization VS Function Overloading(模板专业化 VS 函数重载)

    2. <i id='KY5An'><tr id='KY5An'><dt id='KY5An'><q id='KY5An'><span id='KY5An'><b id='KY5An'><form id='KY5An'><ins id='KY5An'></ins><ul id='KY5An'></ul><sub id='KY5An'></sub></form><legend id='KY5An'></legend><bdo id='KY5An'><pre id='KY5An'><center id='KY5An'></center></pre></bdo></b><th id='KY5An'></th></span></q></dt></tr></i><div id='KY5An'><tfoot id='KY5An'></tfoot><dl id='KY5An'><fieldset id='KY5An'></fieldset></dl></div>
        <tbody id='KY5An'></tbody>

        <tfoot id='KY5An'></tfoot>
            • <small id='KY5An'></small><noframes id='KY5An'>

              • <bdo id='KY5An'></bdo><ul id='KY5An'></ul>

                <legend id='KY5An'><style id='KY5An'><dir id='KY5An'><q id='KY5An'></q></dir></style></legend>
                本文介绍了模板专业化 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 or 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?

                为什么更好?或者如果它们相等,为什么会这样?

                Why is it better? Or if they're equal, why is this?

                推荐答案

                简短的故事:尽可能超载,在需要时专注.

                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* 特化是 的特化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 类型并且它有一个高性能的交换然后你只需在 std 中专门化 swap(Foo&, Foo&)命名空间.没问题.

                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. 在您自己的命名空间中创建一个 swap(Foo<T>&, Foo<T>&) 函数,并希望它可以通过 ADL 找到.我说希望"是因为如果标准库像 std::swap(a, b); 一样调用 swap,那么 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 函数重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                Adding types to the std namespace(将类型添加到 std 命名空间)
                Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)

                  <small id='xAmWQ'></small><noframes id='xAmWQ'>

                  <legend id='xAmWQ'><style id='xAmWQ'><dir id='xAmWQ'><q id='xAmWQ'></q></dir></style></legend>
                    <tfoot id='xAmWQ'></tfoot>
                    <i id='xAmWQ'><tr id='xAmWQ'><dt id='xAmWQ'><q id='xAmWQ'><span id='xAmWQ'><b id='xAmWQ'><form id='xAmWQ'><ins id='xAmWQ'></ins><ul id='xAmWQ'></ul><sub id='xAmWQ'></sub></form><legend id='xAmWQ'></legend><bdo id='xAmWQ'><pre id='xAmWQ'><center id='xAmWQ'></center></pre></bdo></b><th id='xAmWQ'></th></span></q></dt></tr></i><div id='xAmWQ'><tfoot id='xAmWQ'></tfoot><dl id='xAmWQ'><fieldset id='xAmWQ'></fieldset></dl></div>
                    • <bdo id='xAmWQ'></bdo><ul id='xAmWQ'></ul>
                          <tbody id='xAmWQ'></tbody>