For the following code, I had to use #ifdef directives to have it compile under both gcc and msvc. Is there a better way to write this w/o #ifdef directives? With gcc, the specialized templates for PushSettings need to be outside the class declaration. However, msvc only seems to except them when they are declared inside the class.
// header file
#include <iostream>
#include <map>
class SettingsManager
{
public:
template <typename T>
void PushSetting(std::string key, T* value);
#ifdef WINDOWS
template<>
void PushSetting<bool>(std::string key, bool* value)
{
ListElement listElement{ ElementType::TYPE_BOOL, value };
m_Settings.insert(std::make_pair(key, listElement));
}
template<>
void PushSetting<std::string>(std::string key, std::string* value)
{
ListElement listElement{ ElementType::TYPE_STRING, value };
m_Settings.insert(std::make_pair(key, listElement));
}
template<>
void PushSetting<RendererAPI::API>(std::string key, RendererAPI::API* value)
{
ListElement listElement{ ElementType::TYPE_RENDERERAPI_API, value };
m_Settings.insert(std::make_pair(key, listElement));
}
#endif
private:
enum class ElementType
{
TYPE_INT,
TYPE_BOOL,
TYPE_STRING,
TYPE_RENDERERAPI_API
};
struct ListElement
{
ElementType m_Type;
void* m_Pointer;
};
private:
std::map<std::string, ListElement> m_Settings;
};
// cpp file
#include "settings.h"
#include "glm.hpp"
#ifndef WINDOWS
template<>
void SettingsManager::PushSetting<int>(std::string key, int* value)
{
ListElement listElement{TYPE_INT, value};
m_Settings.insert(std::make_pair(key, listElement));
}
template<>
void SettingsManager::PushSetting<bool>(std::string key, bool* value)
{
ListElement listElement{TYPE_BOOL, value};
m_Settings.insert(std::make_pair(key, listElement));
}
template<>
void SettingsManager::PushSetting<std::string>(std::string key, std::string* value)
{
ListElement listElement{TYPE_STRING, value};
m_Settings.insert(std::make_pair(key, listElement));
}
template<>
void SettingsManager::PushSetting<RendererAPI::API>(std::string key, RendererAPI::API* value)
{
ListElement listElement{TYPE_RENDERERAPI_API, value};
m_Settings.insert(std::make_pair(key, listElement));
}
#endif
ElementType::
prefix to the used enum values because it isenum class
, not justenum
). – danadam