我正在编写一个通用类,通过字符串键提取 SrcT 类型的内容,将其转换为 TargetT 类型,然后返回。喜欢:
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")
}
TargetT 是从 Converter 类型推导出来的,但是没有关于 SrcT 的线索。
感谢任何帮助。
更新 检查boost/bind.hpp和boost/bind/bind_template.hpp后,好像没有暴露这个东西。
最佳答案
尝试:
typedef typename boost::function_traits<Converter>::arg1_type SrcT;
关于c++ - 如何从 boost::bind 类型获取参数类型(c++11 不可用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25206895/