C++: error: 'optional' in namespace 'std' does not name a template type — wrong -std standard
The standard-library feature you used needs a newer C++ standard than the compiler is currently set to.
in namespace 'std' does not name a type'optional' in namespace 'std' does not name a template type'string_view' is not a member of 'std'no member named 'format' in namespace 'std''filesystem' is not a member of 'std' Meaning
std::optional, std::string_view, std::filesystem (C++17), std::span, std::format (C++20) and many others exist only when compiling with the matching -std= flag. Old compilers default to C++98/11/14. Set the standard in your build system and make sure the compiler is new enough.
Common causes
- Compiler default standard is older than the feature (GCC before 11 defaults to C++14)
-std=c++11hard-coded in a Makefile or CMake- Compiler too old to support the feature
- Missing the header (
<optional>,<string_view>) as well
⚡ Quick fix
- Compile with
-std=c++17(or c++20) - Set
CMAKE_CXX_STANDARDin CMake - Update the compiler if it still fails
Detailed fix by platform
GCC / Clang
g++ -std=c++20 main.cpp -o app— checkg++ --version(GCC 11+ for most of C++20)- With Clang on Linux install a newer
clangand libstdc++
CMake
- Set
set(CMAKE_CXX_STANDARD 20),set(CMAKE_CXX_STANDARD_REQUIRED ON)andset(CMAKE_CXX_EXTENSIONS OFF)
Code examples
Choosing the C++ standard
cmake_minimum_required(VERSION 3.16)
project(app LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(app main.cpp)
# or per target: target_compile_features(app PRIVATE cxx_std_20)How to diagnose
- Standard — Which -std does the build use? (compile with -v)
- Compiler — Is the version new enough?
- Header — Is the header included?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Could not find a package configuration file CMake Error: Could not find a package configuration file provided by "Foo" (requested version 1.2)
- does not name a type C++: error: 'string' does not name a type
- fatal error: No such file or directory C/C++: fatal error: foo.h: No such file or directory — #include not found
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026