This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/69672] New: Useless -Wenum-compare when comparing enum vaue against the same value in enum hack
- From: "wipedout at yandex dot ru" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 04 Feb 2016 14:26:47 +0000
- Subject: [Bug c++/69672] New: Useless -Wenum-compare when comparing enum vaue against the same value in enum hack
- Auto-submitted: auto-generated
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69672
Bug ID: 69672
Summary: Useless -Wenum-compare when comparing enum vaue
against the same value in enum hack
Product: gcc
Version: 5.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: wipedout at yandex dot ru
Target Milestone: ---
I'm trying this on http://gcc.godbolt.org/ the version is "x86 gcc 5.3.0", the
options line is "-O3 -Wall -std=c++11" The code is as follows:
#include <exception>
enum Magic {
One, Two, Three
};
class Base {
public:
virtual Magic GetType() = 0;
};
class ClassOne : public Base {
public:
enum {
MagicValue = One
};
Magic GetType() { return One; }
};
class ClassTwo : public Base {
public:
enum {
MagicValue = Two
};
Magic GetType() { return Two; }
};
template
<class Target>
Target* Cast( Base* from)
{
if( from->GetType() != Target::MagicValue )
throw new std::exception();
return static_cast<Target*>( from );
}
void test()
{
ClassOne obj;
Cast<ClassOne>( &obj );
}
I get the following:
warning: comparison between 'enum Magic' and 'enum ClassOne::<anonymous>'
[-Wenum-compare]
Well, yes, these two are indeed distinct warnings but code uses so-called enum
hack to define a compile-time constant. The enum arising from the enum hack is
a strict subset of the original enum. There's no room for error here. The
warning is useless in this scenario.