帮一个兄弟。这段代码编译,做我想做的。
#include <iostream>
#include <unordered_map>
#include <variant>
#include <vector>
#include <random>
#include <ctime>
class GlobalVariables
{
public:
typedef std::wstring VariableNameType;
/***************************************************************
----------------------------------------------------------------------
Important that my std::variant holds an int
-----------------------------------------------------------------
*****************************************************************/
typedef std::variant<bool,int,float,std::wstring> VariableType;
typedef std::unordered_map<VariableNameType,VariableType> VariableContainerType;
static const VariableType& GetValue(const VariableNameType& name) { return m_variables[name]; }
static void SetValue(const VariableNameType& name, const VariableType value) { m_variables[name] = value; }
protected:
static VariableContainerType m_variables;
};
GlobalVariables::VariableContainerType GlobalVariables::m_variables;
class RandomNumber
{
public:
typedef std::mt19937 EngineType;
static void Seed(const EngineType::result_type seed)
{
m_engine.seed(seed);
}
protected:
static EngineType m_engine;
};
RandomNumber::EngineType RandomNumber::m_engine(std::time(0));
template<typename T>
class RandomNumberGenerator : public RandomNumber
{ };
template<>
class RandomNumberGenerator<int> : public RandomNumber
{
public:
RandomNumberGenerator(const int min, const int max)
:
m_intDistro(min,max)
{}
int operator()() { return m_intDistro(m_engine); }
protected:
std::uniform_int_distribution<int> m_intDistro;
};
template<>
class RandomNumberGenerator<float> : public RandomNumber
{
public:
RandomNumberGenerator(const float min, const float max)
:
m_floatDistro(min,max)
{}
float operator()() { return m_floatDistro(m_engine); }
protected:
std::uniform_real_distribution<float> m_floatDistro;
};
enum class COMPARISON_OP : uint8_t { EE, NE, LT, GT, LE, GE };
enum class BINARY_OP : uint8_t { ADD, SUB, MUL, DIV };
class Expression
{
public:
virtual GlobalVariables::VariableType GetValue() const = 0;
};
class Constant : public Expression
{
public:
virtual GlobalVariables::VariableType GetValue() const
{
return value;
}
GlobalVariables::VariableType value;
};
class Identifier : public Expression
{
public:
virtual GlobalVariables::VariableType GetValue() const override
{
return GlobalVariables::GetValue(name);
}
GlobalVariables::VariableNameType name;
};
class System : public Expression
{
public:
virtual GlobalVariables::VariableType GetValue() const override
{
return GlobalVariables::GetValue(name);
}
GlobalVariables::VariableNameType name;
};
class BinaryOp : public Expression
{
public:
virtual GlobalVariables::VariableType GetValue() const override
{
if(op == BINARY_OP::ADD)
/***************************************************************
----------------------------------------------------------------------
Important that my visitor returns a simple addition if the types are the
same
-----------------------------------------------------------------
*****************************************************************/
return std::visit([](auto a, auto b) -> GlobalVariables::VariableType
{
if constexpr(!std::is_same_v<decltype(a),decltype(b)>)
throw;
else
return a + b;
}, lhs->GetValue(), rhs->GetValue());
else if(op == BINARY_OP::SUB)
return 0;
else if(op == BINARY_OP::MUL)
return 0;
else // Division
return 0;
}
BINARY_OP op;
Expression* lhs;
Expression* rhs;
};
class Comparison
{
public:
bool GetValue() const
{
if(op == COMPARISON_OP::EE)
return lhs->GetValue() == rhs->GetValue();
else if(op == COMPARISON_OP::NE)
return lhs->GetValue() != rhs->GetValue();
else if(op == COMPARISON_OP::LT)
return lhs->GetValue() < rhs->GetValue();
else if(op == COMPARISON_OP::GT)
return lhs->GetValue() > rhs->GetValue();
else if(op == COMPARISON_OP::LE)
return lhs->GetValue() <= rhs->GetValue();
else // Greater or Equal to
return lhs->GetValue() >= rhs->GetValue();
}
COMPARISON_OP op;
Expression* lhs;
Expression* rhs;
};
class Random : public Expression
{
public:
virtual GlobalVariables::VariableType GetValue() const override
{
return possibilities[randomSelector()]->GetValue();
}
mutable RandomNumberGenerator<int> randomSelector;
std::vector<Expression*> possibilities;
};
class Option
{
};
class Event
{
public:
Expression* eventText;
};
class Branch
{
public:
const std::wstring& GetValue() const
{
if(condition->GetValue())
return trueBody;
else
return falseBody;
}
Comparison* condition;
std::wstring trueBody;
std::wstring falseBody;
};
int main()
{
}
可能有很多不必要的代码,所以我突出显示了我认为是重要部分的内容。
但是,如果我将std::variant
更改为持有int64_t
而不是int
(不是"除了",而是"代替"(,即typedef std::variant<bool,int64_t,float,std::wstring> VariableType
程序编译失败。
编译器错误很长:
||=== Build: Debug in Space Adventure 2 (compiler: Mingw-7.3.0) ===|
main.cpp||In instantiation of 'BinaryOp::GetValue() const::<lambda(auto:1, auto:2)> [with auto:1 = bool; auto:2 = bool; GlobalVariables::VariableType = std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >]':|
includec++type_traits|2797|required from 'constexpr bool std::__call_is_nt(std::__invoke_other) [with _Fn = BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>; _Args = {bool, bool}]'|
includec++type_traits|2803| required by substitution of 'template<bool __v> using __bool_constant = std::integral_constant<bool, __v> [with bool __v = std::__call_is_nt<BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>, bool, bool>((std::__result_of_success<std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, std::__invoke_other>::__invoke_type{}, std::__result_of_success<std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, st|
includec++type_traits|2801|required from 'struct std::__call_is_nothrow<std::__invoke_result<BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>, bool, bool>, BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>, bool, bool>'|
includec++type_traits|143|required from 'struct std::__and_<std::__is_invocable<BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>, bool, bool>, std::__call_is_nothrow<std::__invoke_result<BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>, bool, bool>, BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>, bool, bool> >'|
includec++type_traits|2813|required from 'struct std::__is_nothrow_invocable<BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>, bool, bool>'|
includec++bitsinvoke.h|89| [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]|
includec++variant|691|required from 'static constexpr auto std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<_Result_type (*)(_Visitor, _Variants ...)>, std::tuple<_Tail ...>, std::integer_sequence<long long unsigned int, __indices ...> >::_S_apply() [with _Result_type = std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >; _Visitor = BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>&&; _Variants = {std::variant<bo|
includec++variant|656| recursively required from 'static constexpr void std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<_Result_type (*)(_Visitor, _Variants ...), __dimensions ...>, std::tuple<_Variants ...>, std::integer_sequence<long long unsigned int, __indices ...> >::_S_apply_single_alt(_Tp&) [with long long unsigned int __index = 0; _Tp = std::__detail::__variant::_Multi_array<std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::al|
includec++variant|656|required from 'constexpr const std::__detail::__variant::_Multi_array<std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > (*)(BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::alloc|
includec++variant|709|required from 'struct std::__detail::__variant::__gen_vtable<std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t|
includec++variant|1245|required from 'constexpr decltype(auto) std::visit(_Visitor&&, _Variants&& ...) [with _Visitor = BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>; _Variants = {std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >}]'|
main.cpp|133|required from here|
includec++variant|709| in constexpr expansion of 'std::__detail::__variant::__gen_vtable<std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<|
includec++variant|706| in constexpr expansion of 'std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > (*)(BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wcha|
includec++variant|646| in constexpr expansion of 'std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > (*)(BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wcha|
includec++variant|668| in constexpr expansion of 'std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > (*)(BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wcha|
includec++variant|646| in constexpr expansion of 'std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > (*)(BinaryOp::GetValue() const::<lambda(auto:1, auto:2)>&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >&&, std::variant<bool, long long int, float, std::__cxx11::basic_string<wcha|
main.cpp|132|error: could not convert '(((int)a) + ((int)b))' from 'int' to 'GlobalVariables::VariableType {aka std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >}'|
main.cpp||In member function 'virtual GlobalVariables::VariableType BinaryOp::GetValue() const':|
main.cpp|135|error: could not convert '0' from 'int' to 'GlobalVariables::VariableType {aka std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >}'|
main.cpp|137|error: could not convert '0' from 'int' to 'GlobalVariables::VariableType {aka std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >}'|
main.cpp|139|error: could not convert '0' from 'int' to 'GlobalVariables::VariableType {aka std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >}'|
||=== Build failed: 12 error(s), 10 warning(s) (0 minute(s), 1 second(s)) ===|
但我认为真正的关键是这个错误:
main.cpp|132|error: could not convert '(((int)a) + ((int)b))' from 'int' to 'GlobalVariables::VariableType {aka std::variant<bool, long long int, float, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >}'|
我认为这很重要,因为我在任何时间点都没有使用int
,我正在使用int64_t
所以似乎该程序已经隐式地从int64_t
转换为int
,我看不出在哪里或为什么或如何停止它。
使用 Mingw64 7.3.0 编译。
问题出在BinaryOp::GetValue
函数中的 return 语句上。 第一个错误来自return a + b;
行,当展开变体的bool
成员时。 将两个布尔值转换为int
,将它们加在一起,然后使用该 int 创建一个新的GlobalVariables::VariableType
值。 但是,没有明确的转换 从int
到VariableType
.
出于同样的原因,其他错误都来自各种return 0;
语句。
修复非常简单。 将第一个return
更改为
return decltype(a)(a + b);
它将加法的结果转换为与a
相同的类型,这对于在加法发生之前被提升为int
的小类型(bool
、char
、short
及其有符号/无符号变体(是必需的。
对于return 0;
行,您需要决定0
应该是哪种类型(bool? int_64? 浮动?并显式指定该 0 的类型。
转换为VariableType
变体是不明确的。您可以使用一组类型进行简单的测试
#include <cstdint>
#include <string>
void foo(bool);
void foo(int64_t);
void foo(float);
void foo(std::wstring);
int main()
{
foo(0);
}
此代码将无法编译,因为对foo
的重载调用不明确:有许多候选项可以使用隐式转换调用,并且没有一个最佳候选项。实际上,在构造std::variant
时使用相同的逻辑(请参阅此处构造函数 4 的说明(。
在您的原始版本中,int
显然是最佳候选者,因为它不需要从int
转换。一旦你用int64_t
替换int
,所有类型都需要转换,没有赢家。这就是原因
error: could not convert '0' from 'int' to 'GlobalVariables::VariableType
错误以及
error: could not convert '(((int)a) + ((int)b))' from 'int' to 'GlobalVariables::VariableType
错误。