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

            Bug ID: 35256
           Summary: Erroneous const this pointer in evaluation of
                    constexpr constructors
           Product: clang
           Version: 5.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++14
          Assignee: unassignedclangb...@nondot.org
          Reporter: drsa...@gmail.com
                CC: llvm-bugs@lists.llvm.org

When evaluating a constant expression which calls a constexpr constructor to
construct a local const or constexpr variable, the this pointer is erroneously
treated as const in the evaluation of the constructor, leading to erroneous
compilation errors.

These errors go away if the constructor is called for a local that is not
declared const or constexpr. Also initializing variables through default
initializers and other means does not cause this error, only code in the body
of the constructor that refers to this (or implicitly does so by accessing a
local variable)

A minimal example which fails on clang 5.0 is:

struct A {
    int x{};
    constexpr A() = default;
    constexpr A(int x) { 
        *this = A{};
    }
};

constexpr bool foo() {
    const A v{4};
    return v.x == 0;
}
static_assert(foo());

which fails with error:

14 : <source>:14:15: error: static_assert expression is not an integral
constant expression
static_assert(foo());
              ^~~~~
6 : <source>:6:17: note: modification of object of const-qualified type 'const
A' is not allowed in a constant expression
        *this = A{};

This example can be viewed in godbolt at:
https://godbolt.org/g/SMnwBq

The failure seems to be in many versions of the compiler, it fails with both
--std=c++14 and --std=c++17 and on all versions of the compiler I have found
with C++14 support up to and including 5.0.

This seems clearly wrong by my reading of the standard, and is permitted in
other compilers such as gcc

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to