什么是C ++中的可调用对象?

时间:2013-10-09 17:09:03

标签: c++ multithreading object callable

我正在研究提升线程。我发现线程类有一个接受可调用对象的构造函数。什么是可调用对象?

class CallableClass
{
private:
    // Number of iterations
    int m_iterations;

public:

    // Default constructor
    CallableClass()
    {
        m_iterations=10;
    }

    // Constructor with number of iterations
    CallableClass(int iterations)
    {
        m_iterations=iterations;
    }

    // Copy constructor
    CallableClass(const CallableClass& source)
    {
        m_iterations=source.m_iterations;
    }

    // Destructor
    ~CallableClass()
    {
    }

    // Assignment operator
    CallableClass& operator = (const CallableClass& source)
    {
        m_iterations=source.m_iterations;
        return *this;
    }

    // Static function called by thread
    static void StaticFunction()
    {
        for (int i=0; i < 10; i++)  // Hard-coded upper limit
        {
            cout<<i<<"Do something in parallel (Static function)."<<endl;
            boost::this_thread::yield(); // 'yield' discussed in section 18.6
        }
    }

    // Operator() called by the thread
    void operator () ()
    {
        for (int i=0; i<m_iterations; i++)
        {
            cout<<i<<" - Do something in parallel (operator() )."<<endl;
            boost::this_thread::yield(); // 'yield' discussed in section 18.6
        }
    }

};

这如何成为可调用对象?是因为运算符重载还是构造函数或其他东西?

7 个答案:

答案 0 :(得分:13)

可调用对象可以像函数一样调用,语法为object()object(args);也就是说,一个函数指针,或一个重载operator()的类类型的对象。

类中operator()的重载使其可以调用。

答案 1 :(得分:4)

至少有一个重载operator()的对象是callable object,该运算符及其对象可以调用,如函数调用:

CallableClass obj;
obj();

答案 2 :(得分:4)

这里有两个步骤。在C ++标准中,“函数对象”是一个对象,它可以出现在带括号的参数列表的左侧,即指向函数的指针或类型具有一个或多个operator() s的对象。术语“可调用对象”更广泛:它还包括指向成员的指针(不能使用普通函数调用语法调用)。可调用对象是可以传递给std::bind等的东西。参见20.8.1 [func.def]和20.8 [function.objects] / 1.

答案 3 :(得分:2)

可调用对象是operator()重载的类的对象实例:

struct Functor {
    ret_t operator()();
    // ...
}

Functor func;  // func is a callable object

或解除引用的函数指针:

ret_t func() {
   // ...
}

func;  // func converts to a callable object

答案 4 :(得分:2)

从C ++ 17开始,Callable对象实际上是由标准定义的;有关详细信息,请参阅https://en.cppreference.com/w/cpp/named_req/Callable

答案 5 :(得分:1)

在C ++ 11中,可调用元素可以是:

  • 函数指针,
  • 成员函数指针(与上一个指针不同,请选中here
  • 函子类的对象(在其中实现了operator()的类)
  • 匿名函数(Lambdas)
  • 或包装在std::function对象中的任何提及的元素。

这意味着您可以使用上述每个可调用元素来启动std :: thread。看下面的示例代码:

std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

int func()
{
   return std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());
}

class A
{
public:
   int mem_func() 
   { 
      return std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());
   }
};

class B
{
public:
   int operator()()
   {
      return std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());
   }
};

auto lambda = []() { return std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>()); };


void main()
{
   A a;
   B b;

   std::function<int()> f1 = &func;
   std::function<int()> f2 = std::bind(&A::mem_func, &a);
   std::function<int()> f3 = std::bind(&B::operator(), &b);
   std::function<int()> f4 = lambda;

   std::thread t1 = std::thread(func);
   std::thread t2 = std::thread(&A::mem_func, a);
   std::thread t3 = std::thread(&B::operator(), b);
   std::thread t4 = std::thread(lambda);

   std::thread t5 = std::thread(f1);
   std::thread t6 = std::thread(f2);
   std::thread t7 = std::thread(f3);
   std::thread t8 = std::thread(f4);

   t1.join();
   t2.join();
   t3.join();
   t4.join();
   t5.join();
   t6.join();
   t7.join();
   t8.join();
}

答案 6 :(得分:0)

函数对象添加成员函数指针产生所谓的可调用对象。 当我们在c ++ 98/03中时,我们使用类覆盖operator()作为函数。通常,我们称之为类函数。它具有存储函数状态和其他函数的优点。所以它可以。是最重要的概念。对边框,我们称这个样式类函数和其他c样式函数和指针指向c样式函数&#34;函数对象&#34;。 可调用对象只是可以调用&#34;宾语。它包括函数对象和成员函数指针。