c++ - 如何从 boost::bind 类型获取参数类型(c++11 不可用)

标签 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;
  }

  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/

相关文章:

用于 boost::uuid 转换的字符串

c++ - 用 boost spirit 解析一对整数

c++ - Visual Studio 无法打开输入文件

C++ 传递 Boost::log 严重级别作为函数的参数

c++ - IEnumWbemClassObject::Next 方法不起作用

c++ - 使用 boost 创建相对路径

c++ - boost 元组部分迭代?

c++ - 多线程感知模式下的 BOOST 库

c++ - cpp文件中命名空间内常量的定义

c++ - shared_ptr 的引入导致反序列化时出现段错误(使用 boost::serialization)