0

A "sanity check" question for language lawyers / nitpickers:

In this snippet of code:

enum my_enum { AValue1, AValue2 };

using alias_1 = enum my_enum;
using alias_2 = my_enum;

can there be any sort of difference between alias_1 and alias_2? That is, may a compiler treat them differently somehow? My best understanding is that the answer is "no", but a user of mine is experiencing different compiler behavior with the first kind rather than the second kind of statements, when using MSVC 16.8 + CUDA. Something like cannot convert argument 1 from 'my_enum' to 'alias_1'.

Notes:

  • Pathologies like #define enum or name hiding are relevant to address in answers, I suppose, but I'm mostly interested in a "non-pathological" situation.
  • Asking specifically regarding C++11 in case it matters.
  • I'm finding it difficult to create an MWE, since I don't have that compiler myself nor do I usually run Windows.
einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

1

The only difference I can think of is that with the usage of the keyword enum (as in elaborated type specifier), the non-type names will be excluded from name lookup.

(emphasis mine)

The class-name or enum-name in the elaborated type specifier may either be a simple identifier or be a qualified-id. The name is looked up using unqualified name lookup or qualified name lookup, depending on their appearance. But in either case, non-type names are not considered.

E.g.

enum my_enum { AValue1, AValue2 };

int main()
{
    int my_enum;
    using alias_1 = enum my_enum; // works; refers to the enum my_enum
    //using alias_2 = my_enum;    // doesn't work; the non-type name my_enum in main() is found
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • I was implicitly assuming no hiding, but this is a valid answer. Other than hiding, though, is there anything? – einpoklum Feb 09 '21 at 15:17
  • @einpoklum There is name hiding in `using alias_2 = my_enum;`. With the usage of `enum`, non-type names are excluded then name hiding issue goes away. Other than that, I can't think of other differences. – songyuanyao Feb 09 '21 at 15:21
  • Yes, I understand. I should have clarified that's not the crux of what I'm after. So, your answer is fine but see edit. – einpoklum Feb 09 '21 at 15:23