#include <iostream>
#include <type_traits>
template<typename T>
void Print(const T& value) {
if constexpr (std::is_pointer_v<T>) {
std::cout << *value << std::endl;
} else if constexpr (std::is_array_v<T>) {
for (auto v : value) {
std::cout << v << ' ';
}
std::cout << std::endl;
} else {
std::cout << value << std::endl;
}
}
void CheckMode() {
if constexpr (sizeof(void *) == 4) {
std::cout << "32-bit\n";
} else if constexpr (sizeof(void *) == 8) {
std::cout << "64-bit\n";
} else {
std::cout << "Unknown mode\n";
}
}
int main() {
CheckMode();
int value{ 5 };
Print(&value);
int arr[] = { 1, 2, 3, 4, 5 };
Print(arr);
return 0;
}
/*
The code you posted is a C++ program that demonstrates the usage of constexpr in C++. constexpr is a type of keyword in C++ that specifies that a function or a variable is a constant expression. A constant expression is an expression whose value can be computed at compile time, which makes it useful for defining constant values that are required to be computed at compile time.
The program has three functions:
Print(const T& value): This is a template function that prints a value of any type. The function uses if constexpr to check the type of the input value and print it accordingly. If the input is a pointer, it dereferences the pointer and prints the value. If it is an array, it prints all the values in the array. If it is neither a pointer nor an array, it simply prints the value.
ToString(T value): This is another template function that returns a string representation of the input value. If the input is an arithmetic type (integers, floating-point numbers, etc.), it uses std::to_string to convert it to a string. Otherwise, it converts it to a string using the string constructor.
CheckMode(): This function uses if constexpr to check the system architecture (32-bit or 64-bit) and prints a message indicating the mode.
In the main function, the program calls the CheckMode function to print the system architecture, but the rest of the code is commented out.
*/
To embed this project on your website, copy the following code and paste it into your website's HTML: