namespace std:: does not contain optional
Asked Answered
M

1

16

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?

Microscopy answered 14/6, 2020 at 8:15 Comment(3)
Are you compiling with C++17 support? This type was added in C++17. Many compilers still default to C++14.Durbin
Which C++ Standard are you using? Because std::optional is a C++17 feature. Look at this thread Change C++ Standard VSAssail
that fixed it THX.Microscopy
P
20

std::optional requires C++17.

Live on Godbolt.

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

Pavonine answered 14/6, 2020 at 8:34 Comment(3)
In Visual Studio need add flag /std:c++17 to Debug -> Project Properties -> C/C++ -> All options -> Additional OptionsCyler
in Xcode: TARGETS->Build Settings->Apple Clang - Language - C++->C++ Language Dialect, change C++11[+std=c+=11] to C++17[-std=c++17]Indemonstrable
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.Swaggering

© 2022 - 2025 — McMap. All rights reserved.