Skip to content

Commit 10ccfe5

Browse files
committed
CMake: Added dynamic verification of C++14 support
Flag-based check sometimes fails to properly identify does compiler supports C++14 or not.
1 parent df3b034 commit 10ccfe5

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,23 @@ else()
2525
endif()
2626
endif()
2727

28+
function(CheckCPP14Build OUTVAR)
29+
set(CMAKE_CXX_STANDARD 14)
30+
try_compile(${OUTVAR}
31+
${CMAKE_CURRENT_BINARY_DIR}/cmake-checks
32+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/checks/cpp14.cpp
33+
CMAKE_FLAGS -DCMAKE_CXX_FLAGS=${FLAG_CPP14}
34+
)
35+
endfunction()
36+
2837
if(COMPILER_SUPPORTS_CXX14)
29-
message("== Your C++ compiler supports C++14, YMFM emulator will be ENABLED")
38+
CheckCPP14Build(COMPILER_BUILDS_CXX14)
39+
if(COMPILER_BUILDS_CXX14)
40+
message("== Your C++ compiler supports C++14, YMFM emulator will be ENABLED")
41+
else()
42+
message("== Your C++ compiler can't build C++14, YMFM emulator will be DISABLED")
43+
set(COMPILER_SUPPORTS_CXX14 FALSE)
44+
endif()
3045
else()
3146
message("== Your C++ compiler does NOT supports C++14, YMFM emulator will be DISABLED")
3247
endif()

cmake/checks/cpp14.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// A test program to verify C++ compiler for C++14 support
2+
// Based on The Javatar's example: https://stackoverflow.com/questions/30940504/simplest-c-example-to-verify-my-compiler-is-c14-compliant
3+
4+
#include <iostream>
5+
#include <tuple>
6+
#include <cstdint>
7+
#include <assert.h>
8+
#include <functional>
9+
10+
class TestClass
11+
{
12+
public:
13+
static constexpr int test_constant = 42;
14+
15+
static constexpr int test_funk(uint32_t chnum)
16+
{
17+
assert(chnum >= 42);
18+
return chnum;
19+
}
20+
};
21+
22+
auto f() // this function returns multiple values
23+
{
24+
int x = 5;
25+
return std::make_tuple(x, 7); // not "return {x,7};" because the corresponding
26+
// tuple constructor is explicit (LWG 2051)
27+
}
28+
29+
int main()
30+
{
31+
// heterogeneous tuple construction
32+
int n = 1;
33+
TestClass tc;
34+
auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
35+
n = 7;
36+
std::cout << "The value of t is " << "("
37+
<< std::get<0>(t) << ", " << std::get<1>(t) << ", "
38+
<< std::get<2>(t) << ", " << std::get<3>(t) << ", "
39+
<< std::get<4>(t) << ")\n";
40+
41+
int q = tc.test_funk(40);
42+
43+
// function returning multiple values
44+
int a, b;
45+
std::tie(a, b) = f();
46+
std::cout << a << " " << b << "\n";
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)