C++ 如何从boost::bind类型获取参数类型(c+;+;11不可用)
我正在编写一个泛型类,通过字符串键提取SrcT类型的内容,将其转换为TargetT类型,然后返回。比如:C++ 如何从boost::bind类型获取参数类型(c+;+;11不可用),c++,boost,C++,Boost,我正在编写一个泛型类,通过字符串键提取SrcT类型的内容,将其转换为TargetT类型,然后返回。比如: class Foo { public: bool get(const char* key, std::string& str) { if (std::string(key) == "found") { str = "stringA"; return true; } return false; } boo
class Foo
{
public:
bool get(const char* key, std::string& str)
{
if (std::string(key) == "found")
{
str = "stringA";
return true;
}
return false;
}
bool get(const char* key, int& a)
{
a = 100;
return true;
}
};
class Bar
{
public:
template <typename Converter>
typename Converter::result_type extract(const char* key, Converter converter)
{
typedef typename Converter::first_argument_type SrcT; // <- HERE IS THE ERROR
typedef typename Converter::result_type TargetT;
SrcT temp;
if (_foo.get(key, temp))
{
TargetT target = converter(temp);
return target;
}
else
{
throw std::runtime_exception("ah");
}
}
Foo _foo;
};
struct Converters
{
static int toInt(const std::string& str) { return str.size(); }
static float toFloat(int a) { return 100.0 + a; }
};
BOOST_AUTO_TEST_CASE(Nothing)
{
Bar bar;
const int saveHere = bar.extract("found", boost::bind(&Converters::toInt, _1));
BOOST_CHECK_EQUAL(saveHere, 7); // 7=sizeof("stringA")
}
class-Foo
{
公众:
bool-get(const-char*key,std::string和str)
{
如果(标准::字符串(键)=“找到”)
{
str=“stringA”;
返回true;
}
返回false;
}
bool-get(常量字符*键,int&a)
{
a=100;
返回true;
}
};
分类栏
{
公众:
模板
typename转换器::结果类型提取(常量字符*键,转换器)
{
typedef typename转换器::第一个参数\u type SrcT;//尝试:
typedef typename boost::function_traits<Converter>::arg1_type SrcT;
typedef typename boost::function_traits::arg1_type SrcT;
不幸的是,它对我不起作用。错误:在“struct boost::function\u traits”typedef typename boost::function\u traits::arg1\u type SrcT中没有名为“arg1\u type”的类型;