【问题标题】:Which type to use for callable to std::bind使用哪种类型可调用 std::bind
【发布时间】:2015-11-02 17:59:12
【问题描述】:

在函数中,我需要将 std::bind 的可调用对象作为参数传递。 我应该使用什么正确的类型/模板?

void foo(std::function<void(KnownType)> function, WhatShouldThisBe target)
{
    std::bind(function, target);
}

那么预期用途是:

std::shared_ptr<SomeType> bar = std::make_shared<SomeType>();
foo(&SomeType::function, bar);

【问题讨论】:

  • 带有签名void() 的函数不接受参数。错别字?

标签: c++ function templates c++11 bind


【解决方案1】:

类似

template <class T>
void foo(std::function<void(KnownType)> function, T&& target)
{
    std::bind(function, std::forward<T>(target));
}

【讨论】:

  • thx,但是有没有办法在调用foo的时候不指定类型T?(类似于std::bind不需要知道具体类型的方式?)
  • 编译器应该推断出它,所以你应该能够将它称为foo(function, arg)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 2010-12-22
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
  • 2023-03-16
相关资源
最近更新 更多