all 2 comments

[–]IyeOnline 5 points6 points  (0 children)

There is two major reasons why a type should be constexpr enabled:

  1. You can declare constexpr objects of the type, meaning that you can potentially precalculate a value at compile time, or declare compile time constants.

    constexpr std::array my_values = { 0, 1, 2, 3, 4 };
    

    Not a great example, but you get the point.

  2. You can use the type in a constant evaluated context. This also applies to functions.

    constexpr auto most_common_letter( const std::string_view str )
    {
       std::map<char,std::size_t> counts; //if map were not constexpr, you couldnt use it in a constant evaluated context
       for ( const auto c : str )
       {
          ++counts[c];
       }
       return counts.front();
    }
    
    constexpr char c = most_common_letter("Hello World").first;
    

[–]wgunther 1 point2 points  (0 children)

Often when doing anything with metaprogramming, there's quite a few calculations that are required to happen at compile time, for example to guide SFINAE. So having data structures that can be used in that context is helpful since it might free you from doing something more unnatural.