lundi 22 mai 2017

first catch catches any type

this bit of code is driving me nuts:

#include<iostream>
#include<string>

int main()
{
    std::string test = "foo";
    try
    {
        throw test;
    }
    catch (const int &x)
    {
        std::cout << "int " << x << "\n";
    }
    catch (const double &x)
    {
        std::cout << "double " << x  << "\n";
    } 
    catch (const std::string &x)
    {
        std::cout << "string " << x  << "\n";
    }  
    return 0;
}

Nothing crazy here. But the output ...

int 7675456

I tried it on my linux VM, on GDB online and repl-it and it's working fine. I mean I have what I expect:

string foo

I never post on here because I always find a solution. But this time, It looks that I can't figure out a correct way to ask google and I'm just lost. Anybody got a clue?

Aucun commentaire:

Enregistrer un commentaire