SFML/imgui-sfml

'std::optional' is only available from C++17 onwards

Closed this issue · 3 comments

Good morning, i've got the error while building the project with imgui-sfml
The errors is look like this:

In file included from C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:1:
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.h:49:21: error: 'optional' in namespace 'std' does not name a template type
   49 | IMGUI_SFML_API std::optional<sf::Texture>& GetFontTexture();
      |                     ^~~~~~~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.h:49:16: note: 'std::optional' is only available from C++17 onwards
   49 | IMGUI_SFML_API std::optional<sf::Texture>& GetFontTexture();
      |                ^~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:183:10: error: 'optional' in namespace 'std' does not name a template type
  183 |     std::optional<sf::Texture> fontTexture; // internal font atlas which is used if user doesn't set
      |          ^~~~~~~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:183:5: note: 'std::optional' is only available from C++17 onwards
  183 |     std::optional<sf::Texture> fontTexture; // internal font atlas which is used if user doesn't set
      |     ^~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:202:10: error: 'optional' in namespace 'std' does not name a template type
  202 |     std::optional<sf::Cursor> mouseCursors[ImGuiMouseCursor_COUNT];
      |          ^~~~~~~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:202:5: note: 'std::optional' is only available from C++17 onwards
  202 |     std::optional<sf::Cursor> mouseCursors[ImGuiMouseCursor_COUNT];
      |     ^~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp: In function 'bool ImGui::SFML::Init(sf::Window&, const sf::Vector2f&, bool)':
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:233:40: error: 'make_unique' is not a member of 'std'
  233 |     s_windowContexts.emplace_back(std::make_unique<WindowContext>(&window));
      |                                        ^~~~~~~~~~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:233:40: note: 'std::make_unique' is only available from C++14 onwards
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:233:65: error: expected primary-expression before '>' token
  233 |     s_windowContexts.emplace_back(std::make_unique<WindowContext>(&window));


I was trying to use the 17'th cpp standart, but it didn't helped

cmake_minimum_required(VERSION 3.16)

project(example      
  LANGUAGES CXX
  VERSION 1.0
)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
cmake_policy(SET CMP0077 NEW)
set (CMAKE_CXX_STANDART 17)
set (IMGUI_DIR src/include/imgui)

#other code stuff

I was trying to change the standart to 11, but it didn't helped, what might be the problem ?

It looks like i was incredibly stupid and forget to change this line in CMakeLists:

add_compile_options(-limm32 -std=c++17  -IImGUI -IImGUI/backends -IImGUI/src -Wall -Wunused-parameter -Wno-dev -Wformat -Wnarrowing -Wno-sign-conversion -Wno-error-declaration-after-statement)

Now it seems like the first problem was gone, but new appeared

C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:279:55: error: 'const class sf::Window' has no member named 'getNativeHandle'
  279 |                                   return ctx->window->getNativeHandle() == window.getNativeHandle();
      |                                                       ^~~~~~~~~~~~~~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:279:83: error: 'const class sf::Window' has no member named 'getNativeHandle'
  279 |                                   return ctx->window->getNativeHandle() == window.getNativeHandle();
      |                                                                                   ^~~~~~~~~~~~~~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp: In function 'void ImGui::SFML::ProcessEvent(const sf::Window&, const sf::Event&)':
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:293:41: error: 'const class sf::Event' has no member named 'getIf'
  293 |         if (const auto* resized = event.getIf<sf::Event::Resized>()) {
      |                                         ^~~~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:293:67: error: expected primary-expression before ')' token
  293 |         if (const auto* resized = event.getIf<sf::Event::Resized>()) {
      |                                                                   ^
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:296:51: error: 'const class sf::Event' has no member named 'getIf'
  296 |         } else if (const auto* mouseMoved = event.getIf<sf::Event::MouseMoved>()) {
      |                                                   ^~~~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:296:80: error: expected primary-expression before ')' token
  296 |         } else if (const auto* mouseMoved = event.getIf<sf::Event::MouseMoved>()) {
      |                                                                                ^
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:300:59: error: 'const class sf::Event' has no member named 'getIf'
  300 |         } else if (const auto* mouseButtonPressed = event.getIf<sf::Event::MouseButtonPressed>()) {
      |                                                           ^~~~~
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:300:96: error: expected primary-expression before ')' token
  300 |         } else if (const auto* mouseButtonPressed = event.getIf<sf::Event::MouseButtonPressed>()) {
      |                                                                                                ^
C:\bones\bones_source\src\include\imgui-sfml\imgui-SFML.cpp:307:30: error: 'const class sf::Event' has no member named 'getIf'
  307 |                        event.getIf<sf::Event::MouseButtonReleased>()) {

The problem was about SFML version, i was updated SFML to the master branch and now it's building correctly! Problem solved

set (CMAKE_CXX_STANDART 17)

It's CMAKE_CXX_STANDARD, by the way. Another way to do this:

target_compile_features(${TARGET_NAME} PRIVATE cxx_std_17)