Sign in to follow this  

exceptions and implicit conversions

This topic is 3497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


#include <iostream>
#include <sstream>
#include <string>

using namespace std;

template <typename CharType>
class Foo
{
    public:

        operator       basic_string<CharType>()       { return "foo"; }
        operator const basic_string<CharType>() const { return "foo"; }
};

int main(int argc, char** argv)
{
    try
    {
        throw Foo<char>();
    }
    catch(const string& str)
    {
        cout << str << endl;
    }

    return 1;

};



The execution of this code results in an unhandled exception. Why is the implicit conversion not working for the catch clause?

Share this post


Link to post
Share on other sites
Because that's the way C++ exceptions work.

catch(const std::string&){...} will only catch exceptions of type std::string, or exceptions of a type publically derived from std::string.

Share this post


Link to post
Share on other sites

This topic is 3497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this