SFINAE-constrain the GncOption constructor templates.

So that one can't instantiate an invalid constructor.

Unfortunately Swig doesn't understand SFINAE and will try to create the
invalid constructors anyway but at least this generates a compile-time
error when it tries to.
This commit is contained in:
John Ralls 2021-01-04 17:19:02 -08:00
parent 43f4bcb610
commit a602f64b17
2 changed files with 13 additions and 3 deletions

View File

@ -33,7 +33,10 @@ extern "C"
#include <qoflog.h>
}
template <typename ValueType>
template <typename ValueType,
typename std::enable_if_t<!std::is_base_of_v<OptionClassifier,
std::decay_t<ValueType>>,
int>>
GncOption::GncOption(const char* section, const char* name,
const char* key, const char* doc_string,
ValueType value, GncOptionUIType ui_type) :

View File

@ -37,6 +37,7 @@ extern "C"
#include "gnc-option-ui.hpp"
#include "gnc-option-date.hpp"
struct OptionClassifier;
class GncOptionUIItem;
using GncOptionUIItemPtr = std::unique_ptr<GncOptionUIItem>;
struct QofInstance_s;
@ -65,10 +66,16 @@ using GncOptionVariantPtr = std::unique_ptr<GncOptionVariant>;
class GncOption
{
public:
template <typename OptionType>
template <typename OptionType,
typename std::enable_if_t<std::is_base_of_v<OptionClassifier,
std::decay_t<OptionType>>,
int> = 0>
GncOption(OptionType option) :
m_option{std::make_unique<GncOptionVariant>(option)} {}
template <typename ValueType>
template <typename ValueType,
typename std::enable_if_t<!std::is_base_of_v<OptionClassifier,
std::decay_t<ValueType>>,
int> = 0>
GncOption(const char* section, const char* name,
const char* key, const char* doc_string,
ValueType value,