gpt4 book ai didi

c++ - 如何简化boost::static_vistor的派生类

转载 作者:搜寻专家 更新时间:2023-10-31 00:44:21 25 4
gpt4 key购买 nike

template<typename T>
class ClassVariantVisitor : public boost::static_visitor<T>
{
public:
T operator()(int& i) const {
try
{
return boost::lexical_cast<T>(i);
}
catch ( boost::bad_lexical_cast& e)
{
throw e.what();
}
}

T operator()(double& d) const {
try
{
return boost::lexical_cast<T>(d);
}
catch ( boost::bad_lexical_cast& e)
{
throw e.what();
}
}

// ...
};

如您所见,对于每种不同的类型,operator() 的实现代码是完全相同的。有没有一种实用的方法可以简化代码?

谢谢

//根据评论更新//

template<typename T>
class ClassVariantVisitor : public boost::static_visitor<T>
{
public:
T operator()(T& i) const {
try
{
return boost::lexical_cast<T>(i);
}
catch ( boost::bad_lexical_cast& e)
{
throw e.what();
}
}
};

然后编译器(G++)会生成大量的 errors

/////根据 iammilind 的评论更新了 2

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>

using namespace std;

typedef boost::variant<int, double, string> VarIntDoubleString;

// T is the result_type
template<typename T>
class ClassVariantVisitor : public boost::static_visitor<T>
{
public:
template<typename U>
T operator()(U& i) const {
try
{
return boost::lexical_cast<T>(i);
}
catch ( boost::bad_lexical_cast& e)
{
throw e.what();
}
}
};

int main(void)
{
map<string, VarIntDoubleString> mapValuesThree;

// store & retrieve char
mapValuesThree["char_fieldI"] = VarIntDoubleString('c');
char fieldI = boost::apply_visitor(ClassVariantVisitor<char>(), mapValuesThree["char_fieldI"]);
cout << "fieldI: " << fieldI << endl;
}


~/Documents/C++/boost $ g++ -o p192f4 p192f4.cpp -Wall
~/Documents/C++/boost $ ./p192f4
terminate called after throwing an instance of 'char const*'
aborted
~/Documents/C++/boost $ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2

致 iammilind: 如您所见,编译器在编译期间不会生成任何错误或警告。

//根据 Konstantin Oznobihin 的评论更新了 3

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/contains.hpp>
#include <boost/utility/enable_if.hpp>

using namespace std;
template<typename T>
class ClassVariantVisitor : public boost::static_visitor<T>
{
public:
typedef boost::mpl::vector<int, double, string> VarIntDoubleString;

template <class U>
typename boost::enable_if<
typename boost::mpl::contains<VarIntDoubleString, U>::type, T>::type operator()(U &v) const
{
try
{
return boost::lexical_cast<T>(v);
}
catch ( boost::bad_lexical_cast& e)
{
throw e.what();
}
}
};

int main(void)
{
map<string, ClassVariantVisitor::VarIntDoubleString> mapValuesThree;

// store & retrieve double
mapValuesThree["double_fieldJ"] = ClassVariantVisitor<double>::VarIntDoubleString(2.3456);
double fieldJ = boost::apply_visitor(ClassVariantVisitor<double>(), mapValuesThree["double_fieldJ"]);
cout << "fieldJ: " << fieldJ << endl;

}

我不了解 boost::mpl 并且无法使其工作。请引用errors你可以让我如何更正代码,以便我使用你的想法并使其工作。谢谢

最佳答案

按照 iammilind 的建议使用模板化 operator(),但使用 boost::mpl 过滤类型:


template<typename T>
class ClassVariantVisitor : public boost::static_visitor<T>
{
public:
typedef boost::mpl::vector<int, double> source_types;

template <class U>
typename boost::enable_if<
typename boost::mpl::contains<source_types, U>::type,
T
>::type operator()(U &v) const {
try
{
return boost::lexical_cast<T>(v);
}
catch ( boost::bad_lexical_cast& e)
{
throw e.what();
}
};

更新:如果你有 boost::variant 你可以使用它的嵌套类型序列 types 而不是 boost::mpl::vector 并且你不需要在 ClassVariantVisitor 中定义变体,这是一个基于你的代码的更新解决方案:


#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/contains.hpp>
#include <boost/utility/enable_if.hpp>

using namespace std;

typedef boost::variant<int, double, string> VarIntDoubleString;

template<typename T>
class ClassVariantVisitor : public boost::static_visitor<T>
{
public:
template <class U>
typename boost::enable_if<
typename boost::mpl::contains<VarIntDoubleString::types, U>::type, T>::type operator()(U &v) const
{
try
{
return boost::lexical_cast<T>(v);
}
catch ( boost::bad_lexical_cast& e)
{
throw e.what();
}
}
};

int main(void)
{
map<string, VarIntDoubleString> mapValuesThree;

// store & retrieve double
mapValuesThree["double_fieldJ"] = VarIntDoubleString(2.3456);
double fieldJ = boost::apply_visitor(ClassVariantVisitor<double>(), mapValuesThree["double_fieldJ"]);
cout << "fieldJ: " << fieldJ << endl;
}

关于c++ - 如何简化boost::static_vistor的派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8937188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com