[llvm-bugs] [Bug 38472] New: case value is not a constant expression with c++11 and beyond

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Aug 7 09:47:43 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=38472

            Bug ID: 38472
           Summary: case value is not a constant expression with c++11 and
                    beyond
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Driver
          Assignee: unassignedclangbugs at nondot.org
          Reporter: steveire at gmail.com
                CC: llvm-bugs at lists.llvm.org

This code extracted from real code causes clang diagnostics to be issued from
clang-tidy, and from clang++.exe in c++11 mode, but not from clang-cl.exe and
not from clang++.exe in c++98 mode:


    template<typename ENUM>
    class MyEnum
    {
    public:
        MyEnum()
        {
        }
        MyEnum(ENUM)
        {
        }
        operator ENUM() const { return static_cast<ENUM>(0); }
    };

    enum Enum
    {
        SomeValue
    };

    #ifdef BROKEN
    template<typename T>
    #endif
    struct Templ
    {
        Templ()
        {
            switch (m_enumMember)
    #if 0
            switch (static_cast<Enum>(m_enumMember))
    #endif
            {
            case SomeValue:
                break;
            }
        }
        MyEnum<Enum> m_enumMember;
    };

    int main()
    {
    #ifdef BROKEN
        Templ<int> t1;
    #else
        Templ t2;
    #endif
    }

The error only occurs if Templ is a template and instantiated.


C:\dev\src\llvm\build\releaseprefix\bin\clang++.exe -Weverything -std=c++11 -c
-DBROKEN diagerror.cpp
diagerror.cpp
diagerror.cpp:29:14: error: case value is not a constant expression
        case SomeValue:
             ^~~~~~~~~
diagerror.cpp:29:14: note: non-constexpr constructor 'MyEnum' cannot be used in
a constant expression
diagerror.cpp:9:5: note: declared here
    MyEnum(ENUM)
    ^
diagerror.cpp:27:17: warning: enumeration value 'SomeValue' not handled in
switch [-Wswitch]
        switch (m_enumMember)
                ^
1 warning and 1 error generated.


I don't understand why the error only occurs when used in a template. 

It would be good if there were some way to trigger the error with clang-cl.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180807/fb320793/attachment.html>


More information about the llvm-bugs mailing list