在我的C++库中,我有一个类型boost::variant<A,B>和许多算法将此类型作为输入.而不是成员函数我在这种类型上有全局函数,比如void f( boost::variant<A,B>& var ).我知道这也可以通过模板实现,但这不适合我的设计.
这种编程风格我很好:
boost::variant<A, B> v;
f( v );
Run Code Online (Sandbox Code Playgroud)
但是这个库的一些用户不习惯它,并且由于Boost.Variant概念被类型定义隐藏,他们感觉就像在调用v.f().
为了实现这一点,我可以想到两种可能性:1)重写boost::variant和2)重新实现boost::variant和添加我自己的成员函数.我不确定这些想法是否合适.你能帮我个忙吗?还有其他可能吗?
新标准的功能是否会对C++ 11中的boost库实现产生重大影响?
鉴于存在可变参数模板,特别感兴趣的是boost::variant(BOOST_VARIANT_LIMIT_TYPES)和boost::spirit部分库.
有关于此的好文章吗?
有人可以告诉我,我如何获得boost :: Variant处理无序地图?
typedef boost::variant<long, string, double> lut_value;
unordered_map<lut_value, short> table;
我认为有一个用于boost :: variant的哈希函数,对吗?
编译器说:
./src/lookup/HashMap.o:在函数`std :: __ detail :: _ Hash_code_base中,std :: allocator>,double,boost :: detail :: variant :: void_,boost :: detail :: variant :: void_ ,boost :: detail :: variant :: void_,boost :: detail :: variant :: void_,boost :: detail :: variant :: void_,boost :: detail :: variant :: void_,boost :: detail: :variant :: void_,boost :: detail :: variant :: void_,boost :: detail :: variant :: void_,boost :: detail …
我有一个类需要一个boost :: variant包含各种类型的共享指针,如下所示:
template <typename ToySharedPtrVariant, typename ColorSharedPtrVariant>
class ToyPicker {
typedef std::pair<
ToySharedPtrVariant,
ColorSharedPtrVariant
> toyAndColorPair;
typedef std::map<
std::string,
std::vector<
toyAndColoPair
>
> stringToToyColorPairMap;
// ... methods that use the defined types...
}
Run Code Online (Sandbox Code Playgroud)
该类目前需要以下表单的模板参数进行编译:
ToyPicker<
boost::variant<
boost::shared_ptr<ToyModel>
>,
boost::variant<
boost::shared_ptr<BlueToy>,
boost::shared_ptr<RedToy>,
boost::shared_ptr<GreenToy>
>
> toyPicker;
Run Code Online (Sandbox Code Playgroud)
如何使用mpl列表以便我可以为用户提供以下更简单的定义,然后将其转换为上面类实现中的示例格式?
ToyPicker<
boost::mpl::list<
ToyModel
>,
boost::mpl::list<
BlueToy,
RedToy,
GreenToy
>
> toyPicker;
Run Code Online (Sandbox Code Playgroud) 我正在尝试boost-variant自定义课程。我知道访问类内容的安全方法是使用boost::static_visitor. 你知道为什么下面的代码不能编译吗?对签名/声明有什么要求吗?boost::static_visitor使用时
我发现这个问题Why can't I access this custom type with boost::variant? 但我没明白。
问候
AFG
#include <iostream>
#include <algorithm>
#include <boost/variant.hpp>
struct CA{};
struct ca_visitor : public boost::static_visitor<CA>
{
const CA& operator()(const CA& obj ) const { return obj;}
};
struct CB{};
struct cb_visitor : public boost::static_visitor<CB>
{
const CB& operator()(const CB& obj) const { return obj;}
};
int main(){
typedef boost::variant<
CA
,CB > v_type;
v_type v;
const CA& a = boost::apply_visitor( ca_visitor(), …Run Code Online (Sandbox Code Playgroud) 我的虚函数可以返回单个T或std::vector<T>.boost::variant<T, std::vector<T>>在这种情况下返回是一个好主意还是总是返回std::vector<T>?哪个看起来更好,性能开销最小?返回单个值时使用变量会更快吗?
如何检查特定类型是否typename T可以作图从参数typename ...Args的方式T{Args...}?我知道std::is_constructible< T, Args... >来自的类型特征<type_traits>,但它适用于括号,而不是花括号。我在编写类型特征方面没有太多经验,因此无法提供初始示例。作为简化,我们可以接受任何合理的断言,即使这不会导致一般性的太大损失。
我想测试一个简单的事情,如下所示:
#include <iostream>
#include <boost/variant.hpp>
template<typename T1,typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1,T2>& dt){
os << dt.first << dt.second;
return os;
}
int main(){
boost::variant<int, std::pair<int,int>, bool> v;
v = std::pair<int,int>(3,3);
std::cout << v << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这实际上应该有效,因为对于普通类型,int, double等等,它会编译.
boost::variant有一个打印机vistor,它在内部使用它来输出内容到流.实际上这无法编译,但我真的不知道这个问题:
代码在这里失败:在variant_io.hpp中
template <typename OStream>
class printer
: public boost::static_visitor<>
{
private: // representation
OStream& out_;
public: // structors
explicit printer(OStream& out)
: out_( out )
{
}
public: // visitor interface
template <typename T>
void operator()(const T& …Run Code Online (Sandbox Code Playgroud) 在我正在积极使用的一个项目中boost::variant,我偶然发现了一个我无法自行解决的问题.我有一个boost::variant可能包含原子数据类型和这些原子数据类型的STL容器.
现在,我想计算先前定义的boost::variant类型的实例的大小.基本上只有两种可能的功能.原子数据类型的类型只是1,而STL容器的大小定义为其中包含的元素数.
只有2个原子数据表我实现了以下代码:
#include <boost/variant.hpp>
#include <string>
#include <iostream>
#include <vector>
typedef boost::variant<int, double, std::vector<int>, std::vector<double> > TVariant;
struct sizeVisitor : boost::static_visitor<size_t> {
size_t operator()(int&) {
return 1;
}
size_t operator()(double&) {
return 1;
}
size_t operator()(std::vector<int>& c) {
return c.size();
}
size_t operator()(std::vector<double>& c) {
return c.size();
}
} ;
int main(int argc, char **args) {
sizeVisitor visitor;
TVariant var=5;
std::cout << boost::apply_visitor(visitor, var) << std::endl;
std::vector<int> vector;
vector.push_back(6);
vector.push_back(2);
var=vector;
std::cout << …Run Code Online (Sandbox Code Playgroud) 考虑以下使用的代码boost::variant(但也应该完美适用std::variant)。
#include <vector>
#include <boost/variant.hpp>
int main()
{
boost::variant<std::vector<int>, std::vector<double> > vr
= std::vector<int>(5, 5);;
// OK, no problem.
boost::apply_visitor([](auto a) { std::cout << a[0] << "\n"; }, vr);
// ERROR: return types must not differ.
//boost::apply_visitor([](auto a) { return a.begin(); }, vr);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我们有一个变体,它消耗不同类型的标准向量(例如,int在double这个例子中),并且我们希望有一个访问者返回不同类型的对象(在这种情况下,迭代器到底层容器的开头) )。但是,这不会编译,因为显然std::vector<int>::iterator与std::vector<double>::iterator. 有没有一种巧妙的方法可以基本上实现这一点,可能通过额外的间接层?
boost-variant ×10
c++ ×8
boost ×6
c++11 ×3
boost-mpl ×2
boost-spirit ×1
ostream ×1
stl ×1
templates ×1
type-traits ×1
variant ×1