constexpr errors; VS2017 C++ compiler regression?
Asked Answered
C

1

9

Just installed VS2017, which claims to have superior C++14 support since 2015 (which was rudimentary). Gave it a spin on one of my projects which uses constexpr, and noticed what appear to be some regressions.

This code:

struct s
{
    size_t i;
    constexpr s(nullptr_t) noexcept : i(0) {}
};
static_assert(s(nullptr).i == 0, "!!");

Compiles no problem on VS2015 and Clang, but I get a new error in VS2017:

error C2131: expression did not evaluate to a constant
note: failure was caused by unevaluable pointer value
note: while evaluating 's::s(&s{(null)})'

This code looks fine right? Is constexpr meant to have a problem with nullptr?
I'm astonished a regression this basic could appear, I suspect there must be something wrong with my code...

Cummine answered 21/3, 2017 at 1:58 Comment(7)
Even just constexpr s x{nullptr}; fails with nonsense errors (note: while evaluating 's::s(&x)' – what??); obviously a bug.Stepdame
Surprises me that something so simple and trivial could possibly miss the compilers unit-tests...Cummine
Another snippet of code that raises the same error in the simplest possible string-splitting routine ever: while (*tmp) { if (a_delim == *tmp) { count++; last_comma = tmp; } tmp++; }Preponderant
@ibanjo: What is the connection of the code you posted to "the same error"?Avi
@Manu Evans: Firstly, it is usually "simple and trivial" things that miss unit tests. Complicated functionality often interacts with multiple test, triggering at least one of them. Secondly, even though your code is short, the underlying language semantics is not necessarily "simple and trivial".Avi
@AnT I checked and actually misunderstood the post. Sorry.Preponderant
@AnT: Sure, but you must admit, it's a spectacular failure of the unit tests... "never tested nullptr with constexpr, in any context, at all"Cummine
L
1

constexpr constructor(std::nullptr_t) causes "error C2131: expression did not evaluate to a constant"

This issue was reported as a bug in Visual Studio 2017 version 15.1.
There was a variation of another issue reported earlier by the OP (?).

This was fixed in: Visual Studio 2017 version 15.6 Preview 1

Letti answered 12/10, 2018 at 7:9 Comment(1)
Wanted to get this question out of the list of unanswered questions.Letti

© 2022 - 2024 — McMap. All rights reserved.