I'm pretty much a swig novice so I hope that the problem I'm running into is a basic one. My problem is that I can't seem to get swig to compile an unordered_map. Here's a MWE. Note that I eventually want to use the unordered map as a python dictionary.
// UsingUnorderedMap.h
#include <string>
#include <unordered_map>
class UsingUnorderedMap{
public:
UsingUnorderedMap() = default;
const std::unordered_map<std::string, double> &getStringDoubleMap() const;
void setStringDoubleMap(const std::unordered_map<std::string, double> &stringDoubleMap);
private:
std::unordered_map<std::string, double> StringDoubleMap;
};
// UsingUnorderedMap.cpp
#include "UsingUnorderedMap.h"
const std::unordered_map<std::string, double> &UsingUnorderedMap::getStringDoubleMap() const {
return StringDoubleMap;
}
void UsingUnorderedMap::setStringDoubleMap(const std::unordered_map<std::string, double> &stringDoubleMap) {
StringDoubleMap = stringDoubleMap;
}
// UsingUnorderedMap.i
%module UsingUnorderedMap
%{
#include "UsingUnorderedMap.h"
%}
%include "std_string.i"
%include "std_pair.i"
%include "std_unordered_map.i"
namespace std {
%template(StringDoubleDict) unordered_map<string, double>;
}
%include "UsingUnorderedMap.h"
And you can build it with CMake using :
# CMakeLists.txt
include(UseSWIG)
set_property(SOURCE UsingUnorderedMap.i PROPERTY CPLUSPLUS ON)
swig_add_library(UsingUnorderedMap
LANGUAGE python
TYPE MODULE
SOURCES UsingUnorderedMap.i UsingUnorderedMap.h UsingUnorderedMap.cpp)
set_target_properties(UsingUnorderedMap
PROPERTIES LANGUAGE CXX)
target_include_directories(UsingUnorderedMap PUBLIC
${Python_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(UsingUnorderedMap PUBLIC ${Python_LIBRARIES})
install(TARGETS UsingUnorderedMap DESTINATION UsingUnorderedMap)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/UsingUnorderedMap.py DESTINATION UsingUnorderedMap)
The error I get is:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\unordered_map(133): error C2535: 'std::unordered_map<std::string,double,std::less<std::string>,std::allocator<std::pair<const std::string,double>>,std::allocator<std::pair<const std::string,double>>>::unordered_map(unsigned __int64,const std::less<std::string> &,const std::allocator<std::pair<const std::string,double>> &)': member function already defined or declared
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\unordered_map(128): note: see declaration of 'std::unordered_map<std::string,double,std::less<std::string>,std::allocator<std::pair<const std::string,double>>,std::allocator<std::pair<const std::string,double>>>::unordered_map'
D:\LearnSwig\cmake-build-release-visual-studio\src\UsingUnorderedMap\CMakeFiles\UsingUnorderedMap.dir\UsingUnorderedMapPYTHON_wrap.cxx(5059): note: see reference to class template instantiation 'std::unordered_map<std::string,double,std::less<std::string>,std::allocator<std::pair<const std::string,double>>,std::allocator<std::pair<const std::string,double>>>' being compiled
...
Any help or advice you have would be greatly appreciated, thanks.