mardi 1 mars 2016

VS2015 is allowing a static_cast from std::string to char const*

I'm running into a VERY weird problem and I have no idea what is causing it. The code in question is:

std::string foo;
std::string bar = static_cast<char const*>(foo);

This does not compile using GCC, as I expect. See my live sample.

I have a fairly large proprietary code base for an application I'm working on at $DAYJOB. I am compiling this code base using Visual Studio 2015 and using language features up to C++14. The code above is allowed when I compile that code base.

I created a brand new C++ project in VS2015 and put the suspect code there and built it. It properly rejects it:

error C2440: 'static_cast': cannot convert from 'std::string' to 'const char *'
note: No user-defined-conversion operator available that can

I know that global typecast operators are not permitted, which might cause this to happen. But other than that I can't think of why this is being allowed. Either this is a compiler bug or there is something about our code base interfering with the behavior of static_cast.

Anyone have any ideas about what might be causing this? I apologize that I haven't provided much to go on, but honestly this is a huge mystery to me.

Aucun commentaire:

Enregistrer un commentaire