cpp-docs icon indicating copy to clipboard operation
cpp-docs copied to clipboard

Add example in C2154 error reference

Open Rageking8 opened this issue 3 months ago • 4 comments

  • Add new example that generates C2154.
  • In the error index page, change *trait* to __underlying_type as it seems to be the only trait that can cause C2154 (also to ensure both instances of the C2154 error message are identical).
  • Remove link to Compiler Support for Type Traits as there is no mention of __underlying_type. In fact, across the entire repository, the only mention of __underlying_type is in this error topic.
  • Add links to enumeration and underlying_type.

Example

// C2154.cpp
// compile with: /c

struct S {};
enum E {};
enum class EC {};

__underlying_type(S) s;     // C2154
__underlying_type(int) i;   // C2154
__underlying_type(E) e;     // OK
__underlying_type(EC) ec;   // OK
Visual Studio 2022
C:\Test>cl /c C2154.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35217 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

C2154.cpp
C2154.cpp(8): error C2154: 'S': only enumeration type is allowed as an argument to compiler intrinsic type trait '__underlying_type'
C2154.cpp(9): error C2154: 'int': only enumeration type is allowed as an argument to compiler intrinsic type trait '__underlying_type'
Visual Studio 2026 Insiders
C:\Test>cl /c C2154.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.50.35503 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

C2154.cpp
C2154.cpp(8): error C2154: 'S': only enumeration type is allowed as an argument to compiler intrinsic type trait '__underlying_type'
C2154.cpp(9): error C2154: 'int': only enumeration type is allowed as an argument to compiler intrinsic type trait '__underlying_type'

Rageking8 avatar Sep 21 '25 13:09 Rageking8