Привожу минимальный пример класса, содержащего std::variant, который представляет значение варианта (обработка исключений и ошибок опущена для краткости):
Код: Выделить всё
#include
#include
#include
class Value {
public:
Value(const std::string&);
Value(const int&);
template const T& GetValue() const;
private:
std::variant m_value;
};
Value::Value(const std::string& val):m_value(val) {}
Value::Value(const int& val):m_value(val) {}
template const int& Value::GetValue() const {
return std::get(m_value);
}
template const std::string& Value::GetValue() const {
return std::get(m_value);
}
int main() {
Value v_int { 666 };
Value v_str { "Hola" };
// int i = v_int.GetValue(); // ERROR
int i = v_int.GetValue(); // OK but not desireable
// std::string s = v_str.GetValue(); // ERROR
std::string s = v_str.GetValue(); // OK but not desireable
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79161773/candidate-template-ignored-couldnt-infer-template-argument-t-with-stdvaria[/url]
Мобильная версия