feat: test constexpr
This commit is contained in:
parent
1c9b7cb3f4
commit
730633f15a
4 changed files with 40 additions and 1 deletions
|
@ -12,7 +12,8 @@ include(ExportCompileCommands)
|
||||||
configure_file("${PROJECT_SOURCE_DIR}/include/version.h.in"
|
configure_file("${PROJECT_SOURCE_DIR}/include/version.h.in"
|
||||||
"${PROJECT_BINARY_DIR}/include/version.h")
|
"${PROJECT_BINARY_DIR}/include/version.h")
|
||||||
|
|
||||||
add_executable(hello src/main.cpp src/null.cpp)
|
add_executable(hello src/main.cpp src/null.cpp src/constexp.cpp)
|
||||||
|
|
||||||
target_compile_features(hello PUBLIC cxx_std_20)
|
target_compile_features(hello PUBLIC cxx_std_20)
|
||||||
|
|
||||||
target_include_directories(hello PRIVATE "${PROJECT_BINARY_DIR}/include")
|
target_include_directories(hello PRIVATE "${PROJECT_BINARY_DIR}/include")
|
||||||
|
|
27
src/constexp.cpp
Normal file
27
src/constexp.cpp
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#include "constexp.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
constexpr unsigned int fibonacci(const unsigned int n) {
|
||||||
|
if (n == 1 || n == 2) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fibonacci(n - 2) + fibonacci(n - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void constexp::test() {
|
||||||
|
std::cout << "Fibonacci(10): ";
|
||||||
|
for (const unsigned int n : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {
|
||||||
|
std::cout << fibonacci(n) << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
int arr_1[fibonacci(10)];
|
||||||
|
std::cout << "Array is " << sizeof(arr_1) / sizeof(int) << " items long"
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
constexpr unsigned int arr_2_len = 10;
|
||||||
|
int arr_2[arr_2_len];
|
||||||
|
std::cout << "Array is " << sizeof(arr_2) / sizeof(int) << " items long"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
5
src/constexp.h
Normal file
5
src/constexp.h
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace constexp {
|
||||||
|
void test();
|
||||||
|
};
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
|
||||||
|
#include "constexp.h"
|
||||||
#include "null.h"
|
#include "null.h"
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
@ -10,8 +11,13 @@ int main(int argc, char **argv) {
|
||||||
std::cout << "Running " << PROJECT_NAME << ", version " << VERSION << "\n"
|
std::cout << "Running " << PROJECT_NAME << ", version " << VERSION << "\n"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
|
std::cout << "Null test:" << std::endl;
|
||||||
null::test();
|
null::test();
|
||||||
|
|
||||||
|
std::cout << "\n"
|
||||||
|
<< "Constexpr test:" << std::endl;
|
||||||
|
constexp::test();
|
||||||
|
|
||||||
std::cout << "\nExiting" << std::endl;
|
std::cout << "\nExiting" << std::endl;
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
Loading…
Reference in a new issue