Не получается правильно сконструировать класс для статической инициализации map<T1,T2>
, ругается на синтаксис и просит точку с запятой там где не надо.
В общем запутался в синтаксисе..
Header.h
typedef std::map<long, std::wstring> HresMap;
HresMap HresTest;
template <typename T, typename U>
class InitStaticMap2
{
private:
std::map<T, U> _map;
public:
InitStaticMap2::InitStaticMap2(std::map<T, U>& map)
{
_map = map;
return *this;
}
InitStaticMap2<T, U>& operator() (const T& key, const U& val) const
{
_map[key] = val;
return *this;
}
operator std::map<T, U>()
{
return _map;
}
};
Source.cpp
#include "Header.h"
InitStaticMap2<long, std::wstring>(HresTest)
(123L, L"aaa")
(124L, L"baa")
(125L, L"aba");
вот оригинальный класс, в таком виде собирается и работает, VS2017
Header.h
namespace ErrorList
{
template <typename T, typename U>
class InitStaticMap
{
private:
std::map<T, U> _map;
public:
InitStaticMap::InitStaticMap(const T& key, const U& val)
{
_map[key] = val;
}
InitStaticMap<T, U>& operator()(const T& key, const U& val)
{
_map[key] = val;
return *this;
}
operator std::map<T, U>()
{
return _map;
}
};
typedef std::map<long, std::wstring> HresMap;
HresMap HresultTest = InitStaticMap<long, std::wstring>
(123L, L"aaa")
(124L, L"baa")
(125L, L"aba");
}
HresMap HresTest;
вызовет ошибки линковки.HresMap HresTest = InitStaticMap1<long, std::wstring>(..)(..)(..);