enumerator duplicated value attribute

97 views
Skip to first unread message

Daniel Gutson

unread,
Sep 7, 2018, 2:55:57 PM9/7/18
to std-proposals
Hi,

   sometimes is important to prevent duplication of enumerators in the enumerations. That's why some compilers such as clang has the -Wduplicate-enum.
However there are cases such as ranges within enums, where enumerators define a category and then are followed by the first element of the category, with the same value.

enum Ranges
{
    CategoryX = 100,
       X1 = CategoryX,
       X2,
       X3
     CategoryY = 200
       // ...
};

In order to allow such cases coexist with the useful diagnostic, I propose adding an enumerator attribute, such as [[allow_duplicate]]

enum Ranges
{
    CategoryX = 100 [[allow_duplicate]],
       X1 = CategoryX,
       X2,
       X3
     CategoryY = 200 [[allow_duplicate]]
       // ...
};

This way, we can have the diagnostic and also explicit the intention that in those cases it is OK to have a repeated value.

Comments?

    Daniel.



--
Who’s got the sweetest disposition?
One guess, that’s who?
Who’d never, ever start an argument?
Who never shows a bit of temperament?
Who's never wrong but always right?
Who'd never dream of starting a fight?
Who get stuck with all the bad luck?

Chris Hallock

unread,
Sep 7, 2018, 4:13:34 PM9/7/18
to ISO C++ Standard - Future Proposals


On Friday, September 7, 2018 at 2:55:57 PM UTC-4, Daniel Gutson wrote:
Hi,

   sometimes is important to prevent duplication of enumerators in the enumerations. That's why some compilers such as clang has the -Wduplicate-enum.
However there are cases such as ranges within enums, where enumerators define a category and then are followed by the first element of the category, with the same value.

enum Ranges
{
    CategoryX = 100,
       X1 = CategoryX,
       X2,
       X3
     CategoryY = 200
       // ...
};

In order to allow such cases coexist with the useful diagnostic, I propose adding an enumerator attribute, such as [[allow_duplicate]]

enum Ranges
{
    CategoryX = 100 [[allow_duplicate]],
       X1 = CategoryX,
       X2,
       X3
     CategoryY = 200 [[allow_duplicate]]
       // ...
};

This way, we can have the diagnostic and also explicit the intention that in those cases it is OK to have a repeated value.

That warning triggers only on implicit duplicates, so you're all good.

Arthur O'Dwyer

unread,
Sep 7, 2018, 9:11:32 PM9/7/18
to ISO C++ Standard - Future Proposals
On Friday, September 7, 2018 at 11:55:57 AM UTC-7, Daniel Gutson wrote:
Hi,

   sometimes is important to prevent duplication of enumerators in the enumerations. That's why some compilers such as clang has the -Wduplicate-enum.
However there are cases such as ranges within enums, where enumerators define a category and then are followed by the first element of the category, with the same value.

enum Ranges
{
    CategoryX = 100,
       X1 = CategoryX,
       X2,
       X3
     CategoryY = 200
       // ...
};

What Chris said: this code is already acceptable to Clang.
However, if you do ever run into a situation where Clang is giving you a warning that is "usually correct but I don't want to see it in this particular case," there already exists markup to indicate that situation:

enum Ranges {
    CategoryX = 100,
    X1 = CategoryX,
    X2,
    X3,
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wduplicate-enum"
    DeprecatedNameX1 = X1,
    DeprecatedNameX2,
    DeprecatedNameX3,
#pragma clang diagnostic pop
};

Unfortunately, Clang currently has what-I-consider-a-bug in that it requires you to put the pragma on the first occurrence of the value, rather than on the duplicate occurrence. I've filed a bug report.

–Arthur

gmis...@gmail.com

unread,
Sep 8, 2018, 8:30:06 PM9/8/18
to ISO C++ Standard - Future Proposals
I haven't ever had a need for this feature, but since clang supports a warning for it, it seems it might be reasonable to have it as a standard feature. Which is what the OP is asking for?
So it's good you provided the information that there is a solution that might work for his compiler, but would you vote for a standard way to do this? I think that's the question.
Reply all
Reply to author
Forward
0 new messages