Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "if constexpr" not compile with Visual Studio 2017 15.3?

The if constexpr syntax introduced with C++17 should work with the /std:c++14 compiler switch, according to this documentation: C++17 Features In Visual Studio 2017 Version 15.3 Preview.

However, it does not work. Instead, the following compiler error is generated:

error C4984: 'if constexpr' is a C++17 language extension

Is the documentation wrong?

If so, how can if constexpr be compiled in Visual Studio 2017 15.3?

like image 332
Spock77 Avatar asked Sep 06 '17 00:09

Spock77


People also ask

Should you use constexpr everywhere?

Yes. I believe putting such const ness is always a good practice wherever you can. For example in your class if a given method is not modifying any member then you always tend to put a const keyword in the end. Now putting constexpr suggests that get() must also be a constexpr .

What is if constexpr in C++?

In a constexpr if statement, the value of condition must be a contextually converted constant expression of type bool (until C++23)an expression contextually converted to bool, where the conversion is a constant expression (since C++23).


1 Answers

Looks like the documentation linked in the question is inaccurate here.

To use if constexpr in Visual Studio 2017, you need to compile with either the /std:c++17 or /std:c++latest switch.

like image 113
Spock77 Avatar answered Nov 14 '22 07:11

Spock77