10

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
  • 177,135
  • 25
  • 101
  • 146
DuckPuppy
  • 103
  • 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) – ElGusanito Jun 14 '20 at 08:18
  • that fixed it THX. – DuckPuppy Jun 14 '20 at 08:25

1 Answers1

13

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
  • 6,731
  • 2
  • 12
  • 32
  • 2
    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