I have a std::map which cannot change at runtime. Thus, I have marked it const I cannot mark it constexpr, since has a non-literal type.
Can I deduce the size of this map at compile time?
#include <map>
#include<string>
int main (){
const std::map <int, std::string> my_map {
{ 42, "foo" },
{ 3, "bar" }
};
constexpr auto items = my_map.size();
return items;
}
This does not compile with the error:
:10:20: error: constexpr variable 'items' must be initialized by a constant expression
constexpr auto items = my_map.size(); ^ ~~~~~~~~~~~~~:10:35: note: non-constexpr function 'size' cannot be used in a constant expression
constexpr auto items = my_map.size();