Skip to content

Instantly share code, notes, and snippets.

@RoyBellingan
Created March 25, 2025 18:12
Show Gist options
  • Save RoyBellingan/de64c2b8ff9c5e18ea8a564b71b80a14 to your computer and use it in GitHub Desktop.
Save RoyBellingan/de64c2b8ff9c5e18ea8a564b71b80a14 to your computer and use it in GitHub Desktop.
test.cpp
//compile with time g++ t2.cpp -std=c++23
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>
#include <chrono>
#include <format>
#include <concepts>
#include <numeric>
// Define a concept for arithmetic types
template<typename T>
concept Arithmetic = std::is_arithmetic_v<T>;
// A template function to add two numbers (requires Arithmetic types)
template<Arithmetic T>
T add(T a, T b) {
return a + b;
}
// A template function to multiply two numbers (requires Arithmetic types)
template<Arithmetic T>
T multiply(T a, T b) {
return a * b;
}
// A generic function to print the elements of any container
template<typename Container>
void printContainer(const Container &c) {
for (const auto &elem : c) {
std::cout << elem << " ";
}
std::cout << "\n";
}
int main() {
std::cout << "Testing compile speed with C++23 on gcc13!\n";
// Create and initialize a vector of integers
std::vector<int> vec(10);
std::iota(vec.begin(), vec.end(), 1); // Fill with 1,2,...,10
std::cout << "Original vector: ";
printContainer(vec);
// Reverse the vector using a standard algorithm
std::reverse(vec.begin(), vec.end());
std::cout << "Reversed vector: ";
printContainer(vec);
// Use template arithmetic functions
int a = 7, b = 3;
auto sum = add(a, b);
auto product = multiply(a, b);
// Format a result string using std::format (C++20/23)
std::string result = std::format("Sum: {}, Product: {}", sum, product);
std::cout << result << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment