samedi 14 mars 2015

Is optimizer correct?


I expect the following program to return 0 all of the time. However with Visual Studio 2013 (Update 4), the program exits 1 in release builds. I'm not sure if this is a bug or if the compiler's optimizer is correct and is relying on some edge behavior. If the CONST macro is turned off, the release exe returns 0. If the optimizer is indeed correct, could I get the reason why it is allowed to emit the code it does?



#if 1
# define CONST const
#else
# define CONST
#endif


class TypeId {
public:
bool operator== (TypeId const & other) const
{
return id == other.id;
}

private:
TypeId (void const * id)
: id(id)
{}

public:
template <typename T>
static TypeId Get ()
{
static char CONST uniqueMemLoc = 0;
return TypeId(&uniqueMemLoc);
}

private:
void const * id;
};


int main(int, char **)
{
typedef int A;
typedef unsigned int B;

if (TypeId::Get<A>() == TypeId::Get<B>()) {
return 1;
}
return 0;
}



Aucun commentaire:

Enregistrer un commentaire