- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在为这个问题挠头。我正在尝试使用 boost::variant 来存储一个值 vector ,其类型将在运行时确定。 vector 的内部类型一旦已知就不会改变。我正在尝试创建一个接口(interface),我的代码可以在其中向 vector 添加一个值,稍后我想将所有值作为 vector 检索。我的代码不会编译。我的方法错了吗?我应该为此工作使用多态性吗?代码如下(不编译):
template <typename T>
class record_value_visitor : public boost::static_visitor<void>
{
public:
record_value_visitor(T v) : _val(v) {}
void operator()(vector<T>& vec) const
{
vec.push_back(this->_val);
}
private:
T _val;
};
class monitor
{
public:
monitor(int type)
{
switch (type) {
case 1:
this->_values = vector<int>();
break;
case 2:
this->_values = vector<string>();
break;
default:
throw new exception("Invalid type");
}
this->_type = type;
}
void add_value(int val)
{
if ( this->_type != 1 ) throw new runtime_error("Invalid type for add_value()");
boost::apply_visitor( record_value_visitor<int>(val), this->_values);
}
void add_value(string val)
{
if ( this->_type != 2 ) throw new runtime_error("Invalid type for add_value()");
boost::apply_visitor( record_value_visitor<string>(val), this->_values);
}
private:
int _type;
boost::variant< vector<int>, vector<double>, vector<string> > _values;
};
int main(int argc, char* argv[])
{
monitor int_mon(1);
monitor str_mon(2);
int_mon.add_value(4);
str_mon.add_value(string("foo"));
return 0;
}
编辑:添加错误信息
1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error C2664: 'void record_value_visitor<T>::operator ()(const std::vector<_Ty> &) const' : cannot convert parameter 1 from 'T1' to 'const std::vector<_Ty> &'
1> with
1> [
1> T=int,
1> _Ty=int
1> ]
1> and
1> [
1> _Ty=int
1> ]
1> Reason: cannot convert from 'T1' to 'const std::vector<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference to function template instantiation 'void boost::detail::variant::invoke_visitor<Visitor>::internal_visit<T>(T &,int)' being compiled
1> with
1> [
1> Visitor=const record_value_visitor<int>,
1> T=T1
1> ]
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<Visitor,VoidPtrCV,T>(int,Visitor &,VoidPtrCV,T *,boost::mpl::false_)' being compiled
1> with
1> [
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1> VoidPtrCV=void *,
1> T=T1
1> ]
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke<Visitor,VoidPtrCV,T1,NoBackupFlag>(int,Visitor &,VoidPtrCV,T *,NoBackupFlag,int)' being compiled
1> with
1> [
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1> VoidPtrCV=void *,
1> NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1> T=T1
1> ]
1> c:\boost_1_46_1\boost\variant\variant.hpp(1776) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl<first_which,first_step,Visitor,VoidPtrCV,boost::variant<T0_,T1,T2>::has_fallback_type_>(const int,const int,Visitor &,VoidPtrCV,boost::mpl::false_,NoBackupFlag,Which *,step0 *)' being compiled
1> with
1> [
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1> VoidPtrCV=void *,
1> T0_=std::vector<int>,
1> T1=std::vector<double>,
1> T2=std::vector<std::string>,
1> NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1> Which=first_which,
1> step0=first_step
1> ]
1> c:\boost_1_46_1\boost\variant\variant.hpp(1787) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::internal_apply_visitor_impl<Visitor,void*>(int,int,Visitor &,VoidPtrCV)' being compiled
1> with
1> [
1> T0_=std::vector<int>,
1> T1=std::vector<double>,
1> T2=std::vector<std::string>,
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1> VoidPtrCV=void *
1> ]
1> c:\boost_1_46_1\boost\variant\variant.hpp(1810) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::internal_apply_visitor<boost::detail::variant::invoke_visitor<Visitor>>(boost::detail::variant::invoke_visitor<Visitor> &)' being compiled
1> with
1> [
1> T0_=std::vector<int>,
1> T1=std::vector<double>,
1> T2=std::vector<std::string>,
1> Visitor=const record_value_visitor<int>
1> ]
1> c:\boost_1_46_1\boost\variant\detail\apply_visitor_unary.hpp(76) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::apply_visitor<const Visitor>(Visitor &)' being compiled
1> with
1> [
1> T0_=std::vector<int>,
1> T1=std::vector<double>,
1> T2=std::vector<std::string>,
1> Visitor=record_value_visitor<int>
1> ]
1> c:\documents and settings\g88791\my documents\visual studio 2010\projects\test_boost\test_boost.cpp(58) : see reference to function template instantiation 'void boost::apply_visitor<record_value_visitor<T>,boost::variant<T0_,T1,T2>>(const Visitor &,Visitable &)' being compiled
1> with
1> [
1> T=int,
1> T0_=std::vector<int>,
1> T1=std::vector<double>,
1> T2=std::vector<std::string>,
1> Visitor=record_value_visitor<int>,
1> Visitable=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>
1> ]
1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error C2664: 'void record_value_visitor<T>::operator ()(const std::vector<_Ty> &) const' : cannot convert parameter 1 from 'T2' to 'const std::vector<_Ty> &'
1> with
1> [
1> T=int,
1> _Ty=int
1> ]
1> and
1> [
1> _Ty=int
1> ]
1> Reason: cannot convert from 'T2' to 'const std::vector<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference to function template instantiation 'void boost::detail::variant::invoke_visitor<Visitor>::internal_visit<T>(T &,int)' being compiled
1> with
1> [
1> Visitor=const record_value_visitor<int>,
1> T=T2
1> ]
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<Visitor,VoidPtrCV,T>(int,Visitor &,VoidPtrCV,T *,boost::mpl::false_)' being compiled
1> with
1> [
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1> VoidPtrCV=void *,
1> T=T2
1> ]
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke<Visitor,VoidPtrCV,T2,NoBackupFlag>(int,Visitor &,VoidPtrCV,T *,NoBackupFlag,int)' being compiled
1> with
1> [
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<int>>,
1> VoidPtrCV=void *,
1> NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1> T=T2
1> ]
1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error C2664: 'void record_value_visitor<T>::operator ()(const std::vector<_Ty> &) const' : cannot convert parameter 1 from 'T0' to 'const std::vector<_Ty> &'
1> with
1> [
1> T=std::string,
1> _Ty=std::string
1> ]
1> and
1> [
1> _Ty=std::string
1> ]
1> Reason: cannot convert from 'T0' to 'const std::vector<_Ty>'
1> with
1> [
1> _Ty=std::string
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference to function template instantiation 'void boost::detail::variant::invoke_visitor<Visitor>::internal_visit<T>(T &,int)' being compiled
1> with
1> [
1> Visitor=const record_value_visitor<std::string>,
1> T=T0
1> ]
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<Visitor,VoidPtrCV,T>(int,Visitor &,VoidPtrCV,T *,boost::mpl::false_)' being compiled
1> with
1> [
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1> VoidPtrCV=void *,
1> T=T0
1> ]
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke<Visitor,VoidPtrCV,T0,NoBackupFlag>(int,Visitor &,VoidPtrCV,T *,NoBackupFlag,int)' being compiled
1> with
1> [
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1> VoidPtrCV=void *,
1> NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1> T=T0
1> ]
1> c:\boost_1_46_1\boost\variant\variant.hpp(1776) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl<first_which,first_step,Visitor,VoidPtrCV,boost::variant<T0_,T1,T2>::has_fallback_type_>(const int,const int,Visitor &,VoidPtrCV,boost::mpl::false_,NoBackupFlag,Which *,step0 *)' being compiled
1> with
1> [
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1> VoidPtrCV=void *,
1> T0_=std::vector<int>,
1> T1=std::vector<double>,
1> T2=std::vector<std::string>,
1> NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1> Which=first_which,
1> step0=first_step
1> ]
1> c:\boost_1_46_1\boost\variant\variant.hpp(1787) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::internal_apply_visitor_impl<Visitor,void*>(int,int,Visitor &,VoidPtrCV)' being compiled
1> with
1> [
1> T0_=std::vector<int>,
1> T1=std::vector<double>,
1> T2=std::vector<std::string>,
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1> VoidPtrCV=void *
1> ]
1> c:\boost_1_46_1\boost\variant\variant.hpp(1810) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::internal_apply_visitor<boost::detail::variant::invoke_visitor<Visitor>>(boost::detail::variant::invoke_visitor<Visitor> &)' being compiled
1> with
1> [
1> T0_=std::vector<int>,
1> T1=std::vector<double>,
1> T2=std::vector<std::string>,
1> Visitor=const record_value_visitor<std::string>
1> ]
1> c:\boost_1_46_1\boost\variant\detail\apply_visitor_unary.hpp(76) : see reference to function template instantiation 'void boost::variant<T0_,T1,T2>::apply_visitor<const Visitor>(Visitor &)' being compiled
1> with
1> [
1> T0_=std::vector<int>,
1> T1=std::vector<double>,
1> T2=std::vector<std::string>,
1> Visitor=record_value_visitor<std::string>
1> ]
1> c:\documents and settings\g88791\my documents\visual studio 2010\projects\test_boost\test_boost.cpp(63) : see reference to function template instantiation 'void boost::apply_visitor<record_value_visitor<T>,boost::variant<T0_,T1,T2>>(const Visitor &,Visitable &)' being compiled
1> with
1> [
1> T=std::string,
1> T0_=std::vector<int>,
1> T1=std::vector<double>,
1> T2=std::vector<std::string>,
1> Visitor=record_value_visitor<std::string>,
1> Visitable=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>
1> ]
1>c:\boost_1_46_1\boost\variant\variant.hpp(832): error C2664: 'void record_value_visitor<T>::operator ()(const std::vector<_Ty> &) const' : cannot convert parameter 1 from 'T1' to 'const std::vector<_Ty> &'
1> with
1> [
1> T=std::string,
1> _Ty=std::string
1> ]
1> and
1> [
1> _Ty=std::string
1> ]
1> Reason: cannot convert from 'T1' to 'const std::vector<_Ty>'
1> with
1> [
1> _Ty=std::string
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(145) : see reference to function template instantiation 'void boost::detail::variant::invoke_visitor<Visitor>::internal_visit<T>(T &,int)' being compiled
1> with
1> [
1> Visitor=const record_value_visitor<std::string>,
1> T=T1
1> ]
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(173) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke_impl<Visitor,VoidPtrCV,T>(int,Visitor &,VoidPtrCV,T *,boost::mpl::false_)' being compiled
1> with
1> [
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1> VoidPtrCV=void *,
1> T=T1
1> ]
1> c:\boost_1_46_1\boost\variant\detail\visitation_impl.hpp(256) : see reference to function template instantiation 'void boost::detail::variant::visitation_impl_invoke<Visitor,VoidPtrCV,T1,NoBackupFlag>(int,Visitor &,VoidPtrCV,T *,NoBackupFlag,int)' being compiled
1> with
1> [
1> Visitor=boost::detail::variant::invoke_visitor<const record_value_visitor<std::string>>,
1> VoidPtrCV=void *,
1> NoBackupFlag=boost::variant<std::vector<int>,std::vector<double>,std::vector<std::string>>::has_fallback_type_,
1> T=T1
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
最佳答案
以下编译并按预期运行:
template<typename T>
struct record_value_visitor : boost::static_visitor<>
{
explicit record_value_visitor(T const& val) : val_(val) { }
void operator ()(std::vector<T>& vec) const
{
vec.push_back(this->val_);
}
template<typename U>
void operator ()(std::vector<U> const&) const { /* noop */ }
private:
T val_;
};
template<typename T>
record_value_visitor<T> make_record_value_visitor(T const& val)
{
return record_value_visitor<T>(val);
}
struct monitor
{
explicit monitor(int const type) : type_(type)
{
switch (type)
{
case 1:
this->values_ = std::vector<int>();
break;
case 2:
this->values_ = std::vector<std::string>();
break;
default:
throw std::exception("Invalid type");
}
}
void add_value(int const val)
{
if (this->type_ != 1)
throw std::runtime_error("Invalid type for add_value()");
boost::apply_visitor(make_record_value_visitor(val), this->values_);
}
void add_value(std::string const& val)
{
if (this->type_ != 2)
throw std::runtime_error("Invalid type for add_value()");
boost::apply_visitor(make_record_value_visitor(val), this->values_);
}
private:
int type_;
boost::variant<
std::vector<int>,
std::vector<double>,
std::vector<std::string>
> values_;
};
int main()
{
monitor int_mon(1);
int_mon.add_value(4);
monitor str_mon(2);
str_mon.add_value("foo");
}
访问者的 operator()
需要为变体中的每种可能类型调用 - 模板化的 record_value_visitor::operator()
是这里的关键。
关于c++ - boost::variant 和 STL 容器——我做错了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6768745/
我创建了一个简单的 Variant 类来存储字符串、整数、 double 等。我正在尝试使用 std::map 类型的映射但我收到了这个奇怪的错误: In file included from /us
我有一个整数数组来检索 std::variant 中的内容。但是编译失败,报错No matching function to call 'get' .您能否解释原因,并提出实现相同目的的可行解决方案?
我的问题涉及 boost::variant 转换与 C++ 中的 std::vector 的混合。在我的项目中,我使用变体作为 SQL 的输出。我将始终只使用一种类型的变体。我想要做的是从变量、变量
(警告:虽然它乍一看可能是一个问题,但这是 而不是 一个初学者级别的问题。如果您熟悉“让强制”这个短语或者您曾经查看过 VBA 规范,请继续阅读。) 假设我有一个 Variant 类型的表达式,我想将
我正在使用一个相当笨拙的 c 接口(interface)来存储集合。 LowLevelStorer 类表示我为此接口(interface)编写的包装器。 Storer 类是一个高级类,它与Data 有
我试图将两个变体组合成一个变体只是为了便于阅读。这是代码: using VariantType_basic = std::variant; using VariantType_vector = std
给定一个类型为 std::variant 的变量,我检查过它不包含C .如何将其转换为 std::variant ? std::variant convert(std::variant value)
使用boost:variant: #include #include #include template boost::variant _tuple_index(size_t i, const
在 answer 中对于这个 SO 问题: What is the equivalent of boost::variant in the C++ standard library? 提到boost:
自从在 Android Gradle 插件 0.13.0 中升级到 gradle 2.1 后,这个问题就出现了,但我一直无法理解为什么有时会记录此警告。 考虑此 block 以根据变体类型重命名 AP
我想为变量实现一个模板方法。 但是根据输入是否为 int 变量(char、short、int),我想处理它与输入为 float 变量(float、double、long double)的情况有所不同。
boost::variant通过 boost::variant<>::types 公开其变体类型列表, 可以方便地与 boost::mpl::for_each 一起使用. std::variant缺少
歌词: 我尝试通过 MPI 实现任务池。所以我需要某种 RPC,但它可以在我的程序的不同部分之间工作,这意味着处理器 A 希望处理器 B 以参数 D 调用函数 C。我们不能像处理线程那样在进程之间传递
目前我的库使用 boost::optional 和 boost::variant。由于 C++17 已经发布,我想添加一个选项,它可以与 boost 和 std 一起使用。 所以我成功地测试了带有 b
假设我有: class TypeA { }; class TypeB { }; typedef boost::variant Type; 没关系: void foo(Type t) { }; int
假设我有一个嵌套的 boost::variant -类型TNested包含一些类型和一些其他 boost::variant类型(它本身不能再次包含 boost::variant types ,因此不会
使用带有 gradle 插件版本 3.3.0-alpha11 的 Android Studio 3.3 Canary 11。尝试同步 gradle 时会抛出以下错误 WARNING: API 'var
我看过这篇关于使用 std::variant 的文章.这是因为以下代码引发了代码分析警告: void CChristianLifeMinistryHtmlView::OnTimer(UINT_PTR
我有一个 Delphi 6 类对象,其中包含 30 个变体的数组,每个变体都通过不同的索引属性公开。例如: property responseCode: integer Index 7
...或在内部快速更改类型 std::variant在源代码中。 下面是列表本身及其容器元素的头文件中的代码。 // HVector.hh class HVector: public std::vec
我是一名优秀的程序员,十分优秀!