constexpr std::vector & std::string

C++20

NOTE: No compiler implements this for now1

1. Description*

With constexpr vector we are able to sort a vector in compile time:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {

    std::cout << std::endl;

    constexpr std::vector myVec {15, -5, 0, 5, 10};
    constexpr std::sort(myVec.begin(), myVec.end());
    for (auto v: myVec) std::cout << v << " ";
    std::cout << "\n\n";

    using namespace std::string_literals;
    constexpr std::vector<std::string> myStringVec{"Stroustrup"s, "Vandevoorde"s,
                                                   "Sutter"s, "Josuttis"s, "Wong"s };
    constexpr std::sort(myStringVec.begin(), myStringVec.end());
    for (auto s: myStringVec) std::cout << s << " ";

    std::cout << "\n\n";

}

No compiler can compile this for now1, but it should work.

Source: https://www.modernescpp.com/index.php/constexpr-vector-and-string-in-c-20

2. Footnotes

Footnotes:

1

28 sep 2021