constexpr int maxValue1 = []()-> int { std::vector myVec = {1, 2, 4, 3}; std::sort(myVec.begin(), myVec.end()); return myVec.back(); }(); // immediately-invoked lambda std::cout << maxValue1 << std::endl; constexpr int maxValue = maxElement(); std::cout << s...
C++20 中大量的算法和容器可以使用constexpr,这意味着你甚至可以再编译期vector<int>进行排序 Algorithms library - cppreference.com 如下: #include <iostream> #include <ranges> #include <vector> #include <unordered_set> #include <algorithm> #include <format> constexpr int maxElement() { std:...