Michele Piccolini
Michele Piccolini

Reputation: 2953

Static map class to value "expression must have a constant value" in std::type_index

I am trying to build a structure in C++ that holds classes as keys and has ints as values. I would like to have this a sort of static map, which I can then use in my program to access the int associated to a certain class easily.

This is the code that I am trying to write:

#include <unordered_map>
#include <typeindex>
#include <typeinfo>
#include "TransformC.h"

// map class -> int
std::unordered_map<std::type_index, int> comp_cols;
// populate map
comp_cols[std::type_index(typeid(TransformC))] = 0;
...

I am on Visual Studio 2017 and I get an "expression must have a constant value" error squiggle at std::type_index.

I don't understand what exactly that error is about, and I don't know whether there are other ways of achieving what I am trying to achieve (maybe some template shenanigans that substitutes at compile time a map<Class> with the right index?)

I get this error only if the code posted above is in file scope (I don't have the error in function scope). I can't understand why the error is there in one case and not in the other one.

Please help!

Upvotes: 0

Views: 28

Answers (0)

Related Questions