Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++/STL Wankery

Name: Anonymous 2011-02-15 19:11

In C++, can I define a typecast operator for converting (both ways) between std::string and const char *?

In the end, I just want to avoid having to declare every function twice:


   void func1(const char *x) { func1(std::string(x)); }
   void func1(std::string x);

   void func2(const char *x) { func2(std::string(x)); }
   void func2(std::string x);

   void func3(const char *x) { func3(std::string(x)); }
   void func3(std::string x);

Name: Anonymous 2011-02-16 23:05

>>9
So it does. I wasn't actively trying to troll. Seems I remembered the issue incorrectly. The issue is that you can return 0 from a function that is supposed to return std::string. Consider:

std::string func(bool val) {
    if (val) return 0;
    std::string str = "a";
    return str;
}

int main(int argc, char *argv[]) {
    std::cout << func(false) << std::endl;
    std::cout << func(true) << std::endl;
    getchar();
    return 0;
}


It gives you an access violation and in a debug build, it sends you into the black pits of their standard libraries, but at least with the proper call stack state.

I remember it not faring so well on VS2008. I think that I ended up with an access violation when attempting to use the string instead. I also think I hit this when I changed a function signature and it was compiling fine so I didn't notice until it fucked up.

Not nearly as bad as confusing function overloading, but still costly if you have no idea what the fuck.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List