0

I'm confused by different errors I have in Visual Studio 2017 (version 15.9.11):

'if constexpr' is a C++17 language extension

and

language feature 'structured bindings' requires compiler flag '/std:c++17'

I know that adding /std:c++17 flag will solve these issues but why are there two different messages? What is a difference between language extension and a compiler flag requirement?

I am more interested about this thing, because I'm writing a game in Unreal Engine 4.24 I can use 'if constexpr', but I can't use 'structured bindings' even though I'm using the same compiler.

Why?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
zompi
  • 268
  • 1
  • 2
  • 10

1 Answers1

3

if constexpr is an exceedingly useful langauge construct from C++17. It's very handy for implementing, for example, many optimizations within the standard library.

As such, when Visual Studio 15.3 initially implemented if constexpr, it was used liberally in their standard library implementation even when compiled in C++14 mode. But since much of that code is in headers which, as far as the compiler is concerned, is part of your source code, that means you get to use it too. To allow for that, they made using if constexpr from C++14 a warning rather than an error, a warning they disabled within their headers with #pragmas.

However, despite MSVC's documentation calling it a warning, it is issued by the compiler as a error, which can be suppressed.

The difference in wording between these two conditions is therefore likely to be born of the fact that the if constexpr "error" is considered to be a "warning", despite presenting itself as an "error" by default.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    However, the OP explicitly says they are getting *errors*, not *warnings*, so this doesn't answer the question. – Jörg W Mittag Jun 05 '20 at 14:12
  • 1
    @JörgWMittag: I'm merely repeating what MSVC's own [release notes say](https://devblogs.microsoft.com/cppblog/c17-progress-in-vs-2017-15-5-and-15-6/): "*“if constexpr” is supported in /std:c++14 with a warning that can be suppressed, delighting template metaprogramming library authors everywhere.*" – Nicol Bolas Jun 05 '20 at 14:43
  • @JörgWMittag And anyway, from a standard perspective they are all just "diagnostics", without differentiating their seriousness. – Deduplicator Jun 05 '20 at 15:03
  • @JörgWMittag: I found further information; they explicitly call it a warning in their documentation, but it *presents* as an error by default. I guess the fact that it can be suppressed is how Microsoft considers them different. – Nicol Bolas Jun 05 '20 at 15:10
  • Oh dear MSVC and their own standards, so they put 'if constexpr' inside c++14! That's explaining a lot. Thank You! – zompi Jun 06 '20 at 06:04