# C++: error: 'optional' in namespace 'std' does not name a template type — wrong -std standard

**Code:** `not a member of std` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang, CMake

The standard-library feature you used needs a newer C++ standard than the compiler is currently set to.

## 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++11` hard-coded in a Makefile or CMake
- Compiler too old to support the feature
- Missing the header (`<optional>`, `<string_view>`) as well

## Quick fix

1. Compile with `-std=c++17` (or c++20)
2. Set `CMAKE_CXX_STANDARD` in CMake
3. Update the compiler if it still fails

## Detailed fix by platform

### GCC / Clang

1. `g++ -std=c++20 main.cpp -o app` — check `g++ --version` (GCC 11+ for most of C++20)
2. With Clang on Linux install a newer `clang` and libstdc++

### CMake

1. Set `set(CMAKE_CXX_STANDARD 20)`, `set(CMAKE_CXX_STANDARD_REQUIRED ON)` and `set(CMAKE_CXX_EXTENSIONS OFF)`

## Code examples

### Choosing the C++ standard

```cmake
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

1. **Standard** — Which -std does the build use? (compile with -v)
2. **Compiler** — Is the version new enough?
3. **Header** — Is the header included?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/std-does-not-name-a-type)
