生成类型化 actor 时出现编译器错误 C2664

Compiler Error C2664 when spawning a typed actor

我在使用以下代码时遇到编译器错误,该代码根据 C++ Actor Framework 提供的示例之一稍作修改。错误描述为:

'void caf::intrusive_ptr_release(caf::ref_counted *)': cannot convert argument 1 from 'caf::typed_actor<caf::typed_mpi<caf::detail::type_list<plus_atom,int,int>,caf::detail::type_list<result_atom,int>,caf::detail::empty_type_list>,caf::typed_mpi<caf::detail::type_list<minus_atom,int,int>,caf::detail::type_list<result_atom,int>,caf::detail::empty_type_list>> ' to 'caf::ref_counted *'   include\caf\intrusive_ptr.hpp   70

这是源代码(同样,从 C++ Actor Framework 示例修改而来):

#include "caf/all.hpp"

using namespace caf;

using plus_atom = atom_constant<atom("plus")>;
using minus_atom = atom_constant<atom("minus")>;
using result_atom = atom_constant<atom("result")>;

using calculator_type = typed_actor<replies_to<plus_atom, int, int>::with<result_atom, int>,
                                    replies_to<minus_atom, int, int>::with<result_atom, int>>;

calculator_type::behavior_type typed_calculator(calculator_type::pointer)
{
    return
    {
        [](plus_atom, int x, int y)
        {
            return std::make_tuple(result_atom::value, x + y);
        },
        [](minus_atom, int x, int y)
        {
            return std::make_tuple(result_atom::value, x - y);
        }
    };
}

int main()
{
    spawn_typed<calculator_type>(typed_calculator);
    shutdown();
}

使用模板化 spawn_typed 调用需要引用实施 class,如下例所示:

#include "caf/all.hpp"

using namespace caf;

using plus_atom = atom_constant<atom("plus")>;
using minus_atom = atom_constant<atom("minus")>;
using result_atom = atom_constant<atom("result")>;

using calculator_type = typed_actor<replies_to<plus_atom, int, int>::with<result_atom, int>,
                                    replies_to<minus_atom, int, int>::with<result_atom, int>>;

class typed_calculator_class : public calculator_type::base
{
protected:
    behavior_type make_behavior() override
    {
        return
        {
            [](plus_atom, int x, int y)
            {
                return std::make_tuple(result_atom::value, x + y);
            },
            [](minus_atom, int x, int y)
            {
                return std::make_tuple(result_atom::value, x - y);
            }
        };
    }
};

int main()
{
    spawn_typed<typed_calculator_class>();
    shutdown();
}

或者,要使用非 class 类型的 actor,只需从原始代码中省略模板参数:

spawn_typed(typed_calculator);