Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6323003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:26:16+00:00 2026-05-24T16:26:16+00:00

A textbook I have notes that you can provide your own implementation for standard

  • 0

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).

My questions are the following:

  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?

  2. Why is it better? Or if they’re equal, why is this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:26:16+00:00Added an answer on May 24, 2026 at 4:26 pm

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

    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*) !!!
    

    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*).


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

    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.

    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!

    You have two options at this point:

    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.

    2. 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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about writing your own init methods in objective-c. I've read
I have the following Spring code, taken from a textbook, that's returning a NotWritablePropertyException.
I have a textbook exercise right after looping and using char values (meaning that
The textbook examples of multiple unpacking assignment are something like: import numpy as NP
Here is the textbook standard way of describing a 'class' or constructor function in
I'm following along in my C# textbook and it says that C# assumes that
There is a question in my programming languages textbook that is as follows: Write
I always thought that parentheses improved readability, but in my textbook there is a
I have a modified textbook example as follows (the book is Understanding Ada---a software
So I have a python program that ends up leaving a .dat file from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.