1
0
Fork 0

feat: test constexpr

main
mandlm 2023-10-18 20:29:41 +02:00
parent 1c9b7cb3f4
commit 730633f15a
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7
4 changed files with 40 additions and 1 deletions

View File

@ -12,7 +12,8 @@ include(ExportCompileCommands)
configure_file("${PROJECT_SOURCE_DIR}/include/version.h.in"
"${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_include_directories(hello PRIVATE "${PROJECT_BINARY_DIR}/include")

27
src/constexp.cpp Normal file
View 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
View File

@ -0,0 +1,5 @@
#pragma once
namespace constexp {
void test();
};

View File

@ -3,6 +3,7 @@
#include "version.h"
#include "constexp.h"
#include "null.h"
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::endl;
std::cout << "Null test:" << std::endl;
null::test();
std::cout << "\n"
<< "Constexpr test:" << std::endl;
constexp::test();
std::cout << "\nExiting" << std::endl;
return EXIT_SUCCESS;