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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:23:28+00:00 2026-05-31T11:23:28+00:00

Based on pp. 8 Free Functions template<typename T> void swap(scoped_ptr<T>& a,scoped_ptr<T>& b) This function

  • 0

Based on pp. 8

Free Functions

template<typename T> void swap(scoped_ptr<T>& a,scoped_ptr<T>& b)

This function offers the preferred means by which to exchange the
contents of two scoped pointers. It is preferable because
swap(scoped1,scoped2) can be applied generically (in templated code)
to many pointer types, including raw pointers and third-party smart
pointers.[2] scoped1.swap(scoped2) only works on smart pointers, not
on raw pointers, and only on those that define the operation.

int* pA = new int(10);
int *pB = new int(20);

boost::swap(pA, pB); // Error: could not deduce template argument for 'boost::scoped_ptr<T> &' from 'int *'

Question> How to swap raw pointers with boost::swap?

  • 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-31T11:23:29+00:00Added an answer on May 31, 2026 at 11:23 am

    I don’t understand why the other answers are telling you not to use boost::swap. The entire purpose of boost::swap is to hide the using std::swap; swap(x, y); business. This works just fine:

    #include <boost/swap.hpp>
    
    int main()
    {
        int* pA = new int(10);
        int *pB = new int(20);
    
        boost::swap(pA, pB);
    
        delete pA;
        delete pB;
    }
    

    Obviously if you haven’t included boost/swap.hpp this won’t work. That’s how you use boost::swap to swap two things. You should always prefer to swap two things in this form!

    What you’re reading is simply stating that boost::scoped_ptr also provides an overload of swap inside the boost namespace, so that this works too:

    #include <boost/scoped_ptr.hpp>
    
    int main()
    {    
        boost::scoped_ptr<int> pA(new int(20));
        boost::scoped_ptr<int> pB(new int(20));
    
        boost::swap(pA, pB);
    }
    

    But it should be clear that this won’t work:

    #include <boost/scoped_ptr.hpp>
    
    int main()
    {
        int* pA = new int(10);
        int *pB = new int(20);
    
        boost::swap(pA, pB);
    
        delete pA;
        delete pB;
    }
    

    Because boost/scoped_ptr.hpp has not provided (and indeed doesn’t have the responsibility to provide) a general implementation of boost::swap. If you want to use boost::swap in general, you must include boost/swap.hpp:

    #include <boost/scoped_ptr.hpp>
    #include <boost/swap.hpp>
    
    int main()
    {
        int* pA = new int(10);
        int *pB = new int(20);
    
        boost::scoped_ptr<int> pC(new int(20));
        boost::scoped_ptr<int> pD(new int(20));
    
        boost::swap(pA, pB);
        boost::swap(pC, pD);
    
        delete pA;
        delete pB;
    }
    

    Like that. If you have Boost available to do, do not fall back to the using std::swap stuff.

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

Sidebar

Related Questions

Is there any good bug tracker based on .NET which is free?
I have a template base class like this: template<typename T, std::size_t Size> class VectorT
i am building this sms notification system, which will send 10 times free sms
this is a question about which class does a free on shared pointers. So
On a recent Java project, we needed a free Java based real-time data plotting
Are there any free tools that implement evidence-based scheduling like Joel talks about ?
I am currently investigating several free/open source OpenGL based 3D engines, and was wondering
Does any know of any free or cheap OSS browser based Flash video player?
Does anyone know of a (free?) web-based IDE for .NET development online?
is there a .net based comet engine? like Ajax Push Engine that is free

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.