• Advertisement
Sign in to follow this  

exceptions and implicit conversions

This topic is 3571 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
Advertisement
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
Sign in to follow this  

  • Advertisement