12

i am doing the Vulkan Tutorial https://vulkan-tutorial.com/

#define GLFW_INCLUE_VULKAN
#include<GLFW/glfw3.h>
#include<optional>

struct s {
    std::optional<uint32_t> num;//Intellisense Error
};

int main() {
    return 5;
}

I started with an empty project and added includes and libraries; I can compile and run without including std::optional.

When i use std::optional I get c2039 "optional is not a member of std"

I am running Windows 10, and VisualStudio 2019

What is going on here ?

thx.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
DuckPuppy
  • 123
  • 1
  • 1
  • 7
  • 2
    Are you compiling with C++17 support? This type was added in C++17. Many compilers still default to C++14. – cdhowie Jun 14 '20 at 08:18
  • 1
    Which C++ Standard are you using? Because `std::optional` is a C++17 feature. Look at this thread [Change C++ Standard VS](https://stackoverflow.com/questions/41308933/how-to-enable-c17-compiling-in-visual-studio) – cdecompilador Jun 14 '20 at 08:18
  • that fixed it THX. – DuckPuppy Jun 14 '20 at 08:25

1 Answers1

16

std::optional requires C++17.

Live on Godbolt.

you can use /std:c++17 flag on MSVC and -std=c++17 on gcc/clang.

Community
  • 1
  • 1
Oblivion
  • 7,176
  • 2
  • 14
  • 33
  • 3
    In Visual Studio need add flag `/std:c++17` to Debug -> Project Properties -> C/C++ -> All options -> Additional Options – Elmir Dec 04 '21 at 22:32
  • in Xcode: `TARGETS`->`Build Settings`->`Apple Clang - Language - C++`->`C++ Language Dialect`, change `C++11[+std=c+=11]` to `C++17[-std=c++17]` – crifan Feb 10 '23 at 14:25
  • I had the problem after manually adding `-std=gnu++17` in cmake, while the esp-idf build system itself added `-std=gnu++11` later in the command list overwriting my own. I had to hack the esp-idf scripts to disable the latter. – Mark Jeronimus Aug 02 '23 at 10:13