Search code examples
boostboost-asioc++20

boost asio channels "no type named 'receive_cancelled_signature'"


I'm trying to implement a thread safe observer design pattern using boost so I want to iterate over a list of boost asio channels?

Say I have

struct A{};

std::list<boost::asio::experimental::channel<void(std::error_code, std::shared_ptr<A>>> channels;

for (auto it = channels.begin(); it != channels.end(); ++it)
{}

Using this code I got :

error, no type named 'receive_cancelled_signature'.

When I remove the for loop, the program compiles without errors Is it possible to iterate over channels in boost asio ?


Solution

  • Simpler repro: Live On Coliru

    #include <boost/asio/experimental/channel.hpp>
    #include <boost/asio/system_executor.hpp>
    
    using Channel = boost::asio::experimental::channel<void(std::error_code, size_t)>;
    
    int main() {
        Channel c(boost::asio::system_executor{});
    }
    

    shared_ptr, A, list and the loop had nothing to do with it.

    Your mistake is that you use std::error_code which isn't supported by default.

    Standalone Asio does use std::error_code, and perhaps Boost Asio can be configured to do so.

    Keep in mind that Boost System has more features, and excellent interop with std::error_code these days.

    Fix

    Use boost::system::error_code:

    Live On Coliru

    #include <boost/asio.hpp>
    #include <boost/asio/experimental/channel.hpp>
    #include <iostream>
    #include <list>
    
    using Channel = boost::asio::experimental::channel<void(boost::system::error_code, size_t)>;
    
    
    int main() {
        boost::asio::io_context ctx;
    
        std::list<Channel> channels;
    
        for (auto n = 5; n--;)
            channels.emplace_back(ctx);
    
        for (Channel& ch : channels) {
            std::cout << "Ch at address " << &ch << std::endl;
        }
    }
    

    Prints e.g.

    Ch at address 0x2047d90
    Ch at address 0x2048130
    Ch at address 0x2048470
    Ch at address 0x20487b0
    Ch at address 0x2048af0