on the simplest, he caught errors, when declaring std :: optional, it swears at the absence of an optional member in the namespace. What could be the problem

#include <optional> int main() { std::optional<char> a; return 0; } 
  • You describe "magical" behavior that cannot be explained. Most likely, you have something to keep back. - AnT 1:31 pm
  • @AnT I just need to initialize an empty variable of type std :: optional, the code says how it is done (peeped in HabrĂ©). But my environment says there is no optional member in the std namespace. And there’s not even a code error, perhaps I’ve got it from a working environment bug, or I’m doing something wrong. As on a stack in 2016 I asked a question, but there was a problem with him in the program and optional itself was in experimental. Now it seems to be in std, and maybe with patches it was moved to another namespace. - Plasmat1x
  • Most likely you simply did not switch your compiler to C ++ 17 mode. - AnT 2:23 pm

2 answers 2

A similar error is generated by Visual Studio 2017 if the C ++ 17 mode is not selected in the project settings ( /std:c++17 ). In the Output window, it is accompanied by the message

 class template optional is only available with C++17 or later. 

enter image description here

I suspect that you are looking at compilation errors in this “new” strange GUI-errors window instead of going to the normal human Output window and looking at the full output of the compiler there. For this reason, you did not see the above message.

  • The Errors window can hardly be called new - it definitely is in the studio 2008, and it appeared, if I'm not mistaken, in the 2005 studio. There, by the way, it is really useful, unlike 2017. - freim

The two most common causes of this compiler message may be.

The first reason is that the compiler does not support std::optional .

The second reason is that you did not include the <optional> header.

It may also be that you included this header in Visual Studio before the stdafx.h header.

  • 1) Visual studio 17 is used 2) include <optional> in the project is present - Plasmat1x
  • one
    @ Plasmat1x Then provide the minimum, ready-to-compile code that demonstrates the problem. - Vlad from Moscow
  • I do not use precompiled headers, the compiler does not swear on the compiler, but when I try to create a variable of type std :: optional, it starts to swear, when I type std :: in the context menu there is no member optional - Plasmat1x
  • @ Plasmat1x Is there an error with the actual compilation? Or is this a problem only with intellisense? - HolyBlackCat
  • @ Plasmat1x This may just be an environment error, that is, in the context menu. - Vlad from Moscow