我想将CRTP pattern与某些锁定机制结合使用以在多线程环境中进行访问同步。非模板错误的模板定义
我的代码如下所示:
//-- CRTP base class with some sync/lock mechanism
template<typename T, typename SYNC>
struct Base {
static std::unordered_map<int, std::string> s_map;
static SYNC s_sync;
};
//-- derived class using CRTP
template<typename SYNC>
struct ProductX : public Base<ProductX<SYNC>, SYNC> {};
//-- static initialisation
template<typename SYNC>
std::unordered_map<int, std::string> Base<ProductX<SYNC>, SYNC>::s_map {
{ 1, "value_1" },
{ 2, "value_2" }
}
但是我得到
error: template definition of non-template
std::unordered_map<int, std::basic_string<char> > Base<ProductX<SYNC>, SYNC>::s_map
编译时。
对静态初始化s_map
引发错误。有人能指出我做错了什么吗?
@Deduplicator - 这不是你标记的内容的重复。如果重复的东西,那么这一个:http://stackoverflow.com/questions/13404695/c-how-to-initialize-static-variables-of-a-partial-template-specialization –