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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:32:04+00:00 2026-06-01T12:32:04+00:00

I have a problem with auto_ptr in Exception classes, that I eventually reduced to:

  • 0

I have a problem with auto_ptr in Exception classes, that I eventually reduced to:

#include <memory>

class MyException
{
    std::auto_ptr<int> m_foo2;
};

int main()
{
    try
    {
        throw MyException();
    }
    catch (const MyException&)
    {

    }
    return 0;
}

This fails to compile with:

/perforce/unstable/test/Common/Exceptions/TestException4.cpp: In
function ‘int main()’:
/perforce/unstable/test/Common/Exceptions/TestException4.cpp:12:
error: no matching function for call to
‘MyException::MyException(MyException)’
/perforce/unstable/test/Common/Exceptions/TestException4.cpp:4: note:
candidates are: MyException::MyException()
/perforce/unstable/test/Common/Exceptions/TestException4.cpp:4: note:
MyException::MyException(MyException&)
/perforce/unstable/test/Common/Exceptions/TestException4.cpp:12:
error: in thrown expression

And the error goes away if I remove the auto_ptr.

Is this because the exception is being copied or assigned? Is there a way of using auto_ptrs in an Exception?

  • 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-06-01T12:32:06+00:00Added an answer on June 1, 2026 at 12:32 pm

    Is this because the exception is being copied or assigned?

    Indeed it is. The standard specifies how an exception is thrown in C++11 15.1/3:

    A throw-expression initializes a temporary object, […].
    The temporary is an lvalue and is used to initialize the variable named in the matching handler.

    The initialisation is done with the implicit copy constructor. This is declared as MyException(MyException&), since there is a member that requires a non-const reference argument (as specified in C++11 12.8/9). The temporary object can’t be bound to a non-const reference, and so the construction fails.

    Is there a way of using auto_ptrs in an Exception?

    If you’re able to use C++11, then you could use unique_ptr instead, and consign auto_ptr to the bowels of history. Your class would have an implicit move constructor, declared as MyException(MyException&&), which could be used to initialise it from a temporary.

    Otherwise, you could throw a non-temporary value:

    MyException ex;
    throw ex;
    

    or you could hack your class to allow initialisation from a const reference by adding an explicit copy constructor and using const_cast or mutable to allow that to copy the auto_ptr – but that is potentially dangerous, and I wouldn’t recommend it.

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

Sidebar

Related Questions

I have class foo that contains a std::auto_ptr member that I would like to
I have the following code #include <iostream> #include <cstddef> #include <string> #include <memory> class
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with fancybox. I want to write a function that will run
i have problem with autorotate on iphone i set up in all classes -
I need to have a class with one activity that is performed once per
I have a C++ class that contains a non-copyable handle. The class, however, must
Say i have a templated class template<class T> class A; template<> class A<int> {
Problem I have a template container MyContainer<std::unique_ptr<Foo>> which has a std::deque<T> and a std::vector<T>
I am designing a wrapper class (a bit similar to std::autoPtr but I have

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.