#include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; }
#include <iostream> int main(int argc, char *argv[]) { std::cout << "Hello World!" << std::endl; return 0; }
#include <iostream> #include <format> int main(int argc, char *argv[]) { std::cout << std::format("Hello World!\n"); return 0; }
#include <print> int main(int argc, char *argv[]) { std::println("Hello World!"); return 0; }
// chaostreff.cppm -> Module Interface File export module chaostreff; namespace ChaosTreff { auto getChaotischeViertelstunde() { return "Neue C++ Features - von Gromit"; } export auto printWelcome() { std::println("{}", getChaotischeViertelstunde); } } // namespace Chaostreff
// chaostreff.cppm -> Module Interface File export module chaostreff; namespace ChaosTreff { auto getChaotischeViertelstunde() { return "Neue C++ Features - von Gromit"; } export auto printWelcome() { std::println("{}", getChaotischeViertelstunde); } } // namespace Chaostreff // main.cpp import chaostreff; int main() { ChaosTreff::printWelcome(); return 0; }
#include <vector> #include <algorithm> #include <fmt/ranges.h> int main(int argc, char *argv[]) { std::vector<int> v{3,2,4,1}; fmt::print("{}\n", v); std::sort(v.begin(), v.end()); fmt::print("{}\n", v); return 0; }
#include <vector> #include <algorithm> #include <fmt/ranges.h> int main(int argc, char *argv[]) { std::vector<int> v{3,2,4,1}; fmt::print("{}\n", v); std::ranges::sort(v); fmt::print("{}\n", v); return 0; }
std::vector<int> v{6, 3, 2, 4, 1}; auto result{ data // [6, 2, 4] | views::filter([](const auto& a){ return a % 2 == 0; })
std::vector<int> v{6, 3, 2, 4, 1}; auto result{ data // [6, 2, 4] | views::filter([](const auto& a){ return a % 2 == 0; }) // [12, 4, 8] | views::transform([](const auto& a){ return a * 2.0; })
std::vector<int> v{6, 3, 2, 4, 1}; auto result{ data // [6, 2, 4] | views::filter([](const auto& a){ return a % 2 == 0; }) // [12, 4, 8] | views::transform([](const auto& a){ return a * 2.0; }) // [4, 8] | views::drop(1)
std::vector<int> v{6, 3, 2, 4, 1}; auto result{ data // [6, 2, 4] | views::filter([](const auto& a){ return a % 2 == 0; }) // [12, 4, 8] | views::transform([](const auto& a){ return a * 2.0; }) // [4, 8] | views::drop(1) // [8, 4] | views::reverse
std::vector<int> v{6, 3, 2, 4, 1}; auto result{ data // [6, 2, 4] | views::filter([](const auto& a){ return a % 2 == 0; }) // [12, 4, 8] | views::transform([](const auto& a){ return a * 2.0; }) // [4, 8] | views::drop(1) // [8, 4] | views::reverse // ["4", "8"] | views::transform( [](const auto& i){ return std::to_string(i); }) };
int add(int a, int b) { return a + b; }
int add(int a, int b) { return a + b; } float add(float a, float b) { return a + b; }
int add(int a, int b) { return a + b; } float add(float a, float b) { return a + b; } std::string add(const std::string& a, const std::string& b) { return a + b; }
template<typename T> T add(const T& a, const T& b) { return a + b; }
template<typename T> concept Addable = requires(T a, T b) { a + b; }; template<Addable T> T add(const T& a, const T& b) { return a + b; }
template<typename T> concept Addable = requires(T a, T b) { a + b; }; auto add(Addable const auto& a, Addable const auto& b) { return a + b; }
(a <=> b) < 0 // true if a < b
(a <=> b) > 0 // true if a > b
(a <=> b) == 0 // true if a == b
// compiler will generate functions auto X::operator<=>(const Y&) const = default;
class Point { int x; int y; public: friend bool operator==(const Point& a, const Point& b) { ... }; friend bool operator< (const Point& a, const Point& b) { ... }; friend bool operator!=(const Point& a, const Point& b) { ... }; friend bool operator<=(const Point& a, const Point& b) { ... }; friend bool operator> (const Point& a, const Point& b) { ... }; friend bool operator>=(const Point& a, const Point& b) { ... }; // other functions };
#include <compare> class Point { int x; int y; public: auto operator<=>(const Point&) = default; // other functions };
#include <format> #include <iostream> int main(int argc, char *argv[]) { std::cout << std::format("Read {0} bytes from {1}!\n", 256, "file.txt"); return 0; }
#include <format> #include <print> int main(int argc, char *argv[]) { std::println("Read {0} bytes from {1}!", 256, "file.txt"); return 0; }
#include <iostream> #include <stacktrace> int nested_func(int c) { std::cout << std::stacktrace::current() << '\n'; return c + 1; } int func(int b) { return nested_func(b + 1); } int main() { std::cout << func(777); }
// compile-time pow for uints consteval std::uint64_t ipow_ct(std::uint64_t base, std::uint8_t exp) {...} constexpr std::uint64_t ipow(std::uint64_t base, std::uint8_t exp) { // use a compile-time friendly algorithm if consteval { return ipow_ct(base, exp); } // use runtime evaluation else { return std::pow(base, exp); } } int main(int, const char* argv[]) { static_assert(ipow(0,10) == 0 && ipow(2,10) == 1024); std::cout << ipow(std::strlen(argv[0]), 3) << '\n'; }