From 5623e0b3409c7f0c7d6c655d32c176a852f00719 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Thu, 5 Dec 2019 14:40:31 +0100 Subject: [PATCH 1/3] Test cpp17 features on the build server. --- tests/CMakeLists.txt | 1 + tests/cpp17/CMakeLists.txt | 9 +++++ tests/cpp17/main.cpp | 72 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 tests/cpp17/CMakeLists.txt create mode 100644 tests/cpp17/main.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f77b4bd25f..adcee5704e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,4 +27,5 @@ add_subdirectory(libslic3r) add_subdirectory(timeutils) add_subdirectory(fff_print) add_subdirectory(sla_print) +add_subdirectory(cpp17) # add_subdirectory(example) diff --git a/tests/cpp17/CMakeLists.txt b/tests/cpp17/CMakeLists.txt new file mode 100644 index 0000000000..4e151ecbf0 --- /dev/null +++ b/tests/cpp17/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.1) + +project(Cpp17Test) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_executable(cpp17test main.cpp) + diff --git a/tests/cpp17/main.cpp b/tests/cpp17/main.cpp new file mode 100644 index 0000000000..2fc60d6356 --- /dev/null +++ b/tests/cpp17/main.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +// Test for nested namespace definition +namespace PrusaSlicer::Cpp17 { + +template class Foo +{ + T m_arg; +public: + + explicit Foo(T &&arg): m_arg{arg} {} +}; + +} // namespace PrusaSlicer::Cpp17 + +template std::string get_type(const T &v); + +template<> std::string get_type(const int &) { return "int"; } +template<> std::string get_type(const double &) { return "double"; } +template<> std::string get_type(const float &) { return "double"; } + +int main() +{ + // ///////////////////////////////////////////////////////////////////////// + // Template argument deduction for class templates + // ///////////////////////////////////////////////////////////////////////// + + auto foo = PrusaSlicer::Cpp17::Foo{1.f}; + + // ///////////////////////////////////////////////////////////////////////// + // Structured bindings: + // ///////////////////////////////////////////////////////////////////////// + + auto my_tuple = std::make_tuple(0.2, 10); + + auto [a, b] = my_tuple; + + std::cout << "a is " << get_type(a) << std::endl; + std::cout << "b is " << get_type(b) << std::endl; + + // ///////////////////////////////////////////////////////////////////////// + // Test for std::apply() + // ///////////////////////////////////////////////////////////////////////// + + auto fun = [] (auto a, auto b) { + std::cout << "a (" << get_type(a) << ") = " << a << std::endl; + std::cout << "b (" << get_type(b) << ") = " << b << std::endl; + }; + + std::apply(fun, my_tuple); + + // ///////////////////////////////////////////////////////////////////////// + // constexpr lambda and if + // ///////////////////////////////////////////////////////////////////////// + + auto isIntegral = [](auto v) constexpr -> bool { + if constexpr (std::is_integral_v) { + return true; + } else { + return false; + } + }; + + static_assert (isIntegral(10), "" ); + // would fail to compile: static_assert (isIntegral(10.0), "" ); + std::cout << "Integer is integral: " << isIntegral(0) << std::endl; + std::cout << "Floating point is not integral: " << isIntegral(0.0) << std::endl; + + return EXIT_SUCCESS; +} From ab7c74245bafb7656e4a6a93b46155ecd9cf89b0 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Thu, 5 Dec 2019 14:46:00 +0100 Subject: [PATCH 2/3] Add cpp17 headers to see if they are available. --- tests/cpp17/main.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/cpp17/main.cpp b/tests/cpp17/main.cpp index 2fc60d6356..385d863a3c 100644 --- a/tests/cpp17/main.cpp +++ b/tests/cpp17/main.cpp @@ -2,6 +2,13 @@ #include #include #include + +// Test new headers in cpp17 +#include +#include +#include +#include + // Test for nested namespace definition namespace PrusaSlicer::Cpp17 { From 1fad91e48508e842191a884be781dec204937814 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Fri, 6 Dec 2019 09:28:17 +0100 Subject: [PATCH 3/3] Get ready to merge with master. --- tests/CMakeLists.txt | 2 +- tests/cpp17/main.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index adcee5704e..458d398602 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,5 +27,5 @@ add_subdirectory(libslic3r) add_subdirectory(timeutils) add_subdirectory(fff_print) add_subdirectory(sla_print) -add_subdirectory(cpp17) +add_subdirectory(cpp17 EXCLUDE_FROM_ALL) # does not have to be built all the time # add_subdirectory(example) diff --git a/tests/cpp17/main.cpp b/tests/cpp17/main.cpp index 385d863a3c..f053b4fb46 100644 --- a/tests/cpp17/main.cpp +++ b/tests/cpp17/main.cpp @@ -72,6 +72,7 @@ int main() static_assert (isIntegral(10), "" ); // would fail to compile: static_assert (isIntegral(10.0), "" ); + std::cout << "Integer is integral: " << isIntegral(0) << std::endl; std::cout << "Floating point is not integral: " << isIntegral(0.0) << std::endl;