简体   繁体   中英

How to initialize a static std::unordered_map of a type trait?

Given the following type trait, how can I initialize Fields with some std::pair s?

template <>
struct ManagerDataTrait<Person>
{
    static const std::unordered_map<std::string, std::string> Fields;
    // ...
};

I tried using a lambda but Visual Studio says that Fields is not an entity that can be explicitly specialized.

template <>
const std::unordered_map<std::string, std::string> ManagerDataTrait<Person>::Fields = []{
    std::unordered_map<std::string, std::string> fields;
    fields.insert(std::make_pair("height", "FLOAT"));
    fields.insert(std::make_pair("mass", "FLOAT"));
    return fields;
};

If there is no way to use static members like this in traits, which alternatives do I have to store the information in a trait? ( Fields holds a SQL database structure.)

Update: The member might be also const but that shouldn't be the point.

You realize you can initialize maps from braced lists?

std::unordered_map<std::string, std::string> m { { "a", "bc" }
                                               , { "b", "xy" }
//                                               ...
                                               };

Kerrek SB's answer would be the right answer in general:

const std::unordered_map<std::string, std::string> ManagerDataTrait<Person>::Fields{
  { "blah", "blah" }
  // ...
};

(NB no template<> because you're defining a member of a specialization, not a specialization)

But that isn't supported by Visual C++, so the other alternative is to initialize the map with a function call, and return a map with the desired contents from the function:

std::unordered_map<std::string, std::string>
getFields()
{
  std::unordered_map<std::string, std::string> fields;
  fields["blah"] = "blah";
  // ...
  return fields;
}

const std::unordered_map<std::string, std::string> ManagerDataTrait<Person>::Fields = getFields();

A lambda is just syntactic sugar for doing the same thing, and I'm not sure it's clearer to use a lambda because the syntax is a bit uglier.

Visual C ++现在支持从支撑列表中进行静态初始化,因此您可以执行以下操作:

const std::unordered_map<std::string, std::string> ManagerDataTrait<Person>::Fields{ { "abc", "xyz" }, { "cde", "zyx" } ...};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM