the compiler throws an error on a function from boost.asio with cmake

52 views Asked by At

I'm just learning boost.asio and decided to try to learn how to work with cmake as well, but my IDE swears at the async_wait function

my code looks like this

#include <boost/asio.hpp>
#include <iostream>
#include <functional>

void print(boost::system::error_code& ec) {
    std::cout << "Hello\n";
}

int main() {
    boost::asio::io_context io;
    boost::asio::steady_timer t(io, boost::asio::chrono::seconds(1));

    t.async_wait(std::bind(print, boost::asio::placeholders::error));
}

but the compiler throws this error

E0304 missing instances of the "boost::asio" function template::basic_waitable_timer<Clock, WaitTraits, Executor>::async_wait [with Clock=std::chrono::steady_clock, WaitTraits=boost::asio::wait_traitsstd::chrono::steady_clock, Executor=boost::asio::any_io_executor]" corresponding to the list of arguments

maybe it's because I didn't connect CMakeLists correctly

cmake_minimum_required (VERSION 3.8)

 project ("BoostLer")

 set(BOOST_ROOT "C:\\boost")
 set(BOOST_LIBRARYDIR "${BOOST_ROOT}/lib")
 set(BOOST_INCLUDEDIR "${BOOST_ROOT}/include")

 find_package(boost REQUIRED COMPONENTS system filesystem)
 add_subdirectory("BoostLer")

This is the first CMakeLists

add_executable (BoostLer "BoostLer.cpp" )

 if (CMAKE_VERSION VERSION_GREATER 3.12)
   set_property(TARGET BoostLer PROPERTY CXX_STANDARD 20)
 endif()

 include_directories(${Boost_INCLUDE_DIRS})
 target_link_libraries(BoostLer PUBLIC ${BOOST_LIBRARIES})

this is the second CMakeLists

2

There are 2 answers

0
sehe On

You're using the asio placeholder with std::bind. I think you need to use std::placeholders::_1 instead.

Of course you could use a lambda to avoid bind expressions in the first place.

Also, your program will not have any effect unless you run the io_context.

0
navaneeth mohan On

To further elaborate on sehe's answer

t.async_wait([](const boost::system::error_code ec)
    {
        // do something when timer times out
    }
);