[llvm-bugs] [Bug 42977] New: Please warn on if constexpr (std::is_constant_evaluated())

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Aug 12 12:48:40 PDT 2019


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

            Bug ID: 42977
           Summary: Please warn on if constexpr
                    (std::is_constant_evaluated())
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: barry.revzin at gmail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

A very common user error with std::is_constant_evaluated is to write this:

#include <type_traits>

constexpr int foo(int i) {
    if constexpr (std::is_constant_evaluated()) {
        return 42;
    } else {
        return i;
    }
}

That is, check if we're in the middle of constant evaluation with "if
constexpr" instead of plain "if". This is the intuitive check to do, but
unfortunately, since the condition here is manifestly constant evaluated, the
result of check is unconditionally true. clang implements this correctly - the
above function is exactly equivalent to "return 42;" - but that's unlikely to
be what the user intended the program to do. They likely intended to write:

constexpr int foo(int i) {
    if (std::is_constant_evaluated()) {
        return 42;
    } else {
        return i;
    }
}

Can you please provide a warning for this erroneous usage?

(I filed the same bug report for gcc:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91428)

-- 
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/20190812/b5a0ffeb/attachment.html>


More information about the llvm-bugs mailing list