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 5954005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:54:30+00:00 2026-05-22T17:54:30+00:00

Couple of questions on boost::swap . Please refer to the code below which is

  • 0

Couple of questions on boost::swap. Please refer to the code below which is basically a cut-paste from boost/swap.hpp. I am referring to library version 1.43.0.

namespace boost_swap_impl
    {
      template<class T>
      void swap_impl(T& left, T& right)
      {
        using namespace std;//use std::swap if argument dependent lookup fails
        swap(left,right);
      }

  template<class T, std::size_t N>
  void swap_impl(T (& left)[N], T (& right)[N])
  {
    for (std::size_t i = 0; i < N; ++i)
    {
      ::boost_swap_impl::swap_impl(left[i], right[i]);
    }
  }
}

namespace boost
{
  template<class T1, class T2>
  void swap(T1& left, T2& right)
  {
    ::boost_swap_impl::swap_impl(left, right);
  }
}
  1. Why is boost::swap declared as template <typename T1, typename T2> when in the rest of the code it is all dealing with the same type?
  2. If I define my own global function void swap(T&, T&) I see that it is the global function that gets called from swap_impl(T& left, T& right). Is this not a conflict and hence an error condition since swap_impl also uses namespace std which has swap defined?
  • 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-22T17:54:31+00:00Added an answer on May 22, 2026 at 5:54 pm
    1. This makes it less specialized than std::swap so you don’t get overload ambiguity errors when both std::swap and boost::swap are in scope (std::swap will take precedence).
    2. No, non-templates always have precedence over templates during overload resolution, so a namespace-scoped non-template swap will take precedence over both boost::swap and std::swap (as will a namespace-scoped template swap overloaded for a UDT – think partially-specialized, but not really..). Note that unlike std::swap, boost::swap is written explicitly to take advantage of ADL.

    Here’s what the C++03 standard has to say regarding both points – [over.match.best] (§13.3.3/1):

    Define ICSi(F) as follows:

    • if F is a static member function, ICS1(F) is defined such that ICS1(F) is neither better nor worse than ICS1(G) for any function G, and, symmetrically, ICS1(G) is neither better nor worse than ICS1(F); otherwise,
    • let ICSi(F) denote the implicit conversion sequence that converts the i-th argument in the list to the type of the i-th parameter of viable function F. 13.3.3.1 defines the implicit conversion sequences and 13.3.3.2 defines what it means for one implicit conversion sequence to be a better conversion sequence or worse conversion sequence than another.

    Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then

    • for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that,
    • F1 is a non-template function and F2 is a function template specialization, or, if not that,
    • F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.5.2, or, if not that,
    • the context is an initialization by user-defined conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the return type of F1 to the destination type (i.e., the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the return type of F2 to the destination type.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks to those who answered my last couple questions, I got the following code
I have couple of questions regarding boost licensing, Is it that all the boost
I'm trying to use Kryo to read/write objects to disk: http://code.google.com/p/kryo/ Couple questions: How
Have a couple questions, using GSON. I have a feeling that GSON might not
I have a couple questions about SSL. What happens if someone tampers or changes
I've seen a couple questions on this but haven't been able to solve it...
I have a couple questions about exceptions. 1) when you hit a catch block,
I have couple questions regarding some C++ rules. Why am I able to call
I found a couple questions similar to this, but not specifically for .NET projects,
I have a couple questions about submitting blue-tooth enabled apps on the iPhone. I

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.