Search code examples
c++c++11boostboost-asioboost-timer

How to instantiate the boost class `boost::posix_time::seconds` C++ class?


I found the following class definition in the boost library 1.71.0 which is installed at /usr/include/boost in my case.

class BOOST_SYMBOL_VISIBLE seconds : public time_duration
        {
            public:
                template <typename T>
                    explicit seconds(T const& s,
                            typename boost::enable_if<boost::is_integral<T>, void>::type* = BOOST_DATE_TIME_NULLPTR) :
                        time_duration(0,0, numeric_cast<sec_type>(s))
            {}
        };

The Above class definition can be found out at /usr/include/boost/date_time/posix_time/posix_time_duration.hpp

I am using following code-snippet where the class boost::posix_time::seconds is instantiated:

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/format.hpp>
#include <boost/bind.hpp>
#include <boost/serialization/access.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/shared_array.hpp>
#include <boost/algorithm/string.hpp>
#include <chrono>
#include <vector>
#include <boost/date_time/posix_time/posix_time_duration.hpp>

int main()
{
    boost::asio::io_service io_service;
    boost::asio::ip::tcp::socket socket(io_service);
    boost::asio::deadline_timer timer(io_service);

    timer.expires_from_now(boost::posix_time::seconds(60.0))

    return 0;
}

Let's say I save the above code-snippet with the file name boost.cpp. Then I compile boost.cpp using the command

g++ -I /usr/include/boost  -pthread boost.cpp

However, I am getting the following error:

boost.cpp: In function ‘int main()’:
boost.cpp:19:59: error: no matching function for call to ‘boost::posix_time::seconds::seconds(double)’
   19 |     timer.expires_from_now(boost::posix_time::seconds(60.0))
      |                                                           ^
In file included from /usr/include/boost/date_time/posix_time/posix_time_types.hpp:16,
                 from /usr/include/boost/asio/time_traits.hpp:23,
                 from /usr/include/boost/asio/detail/timer_queue_ptime.hpp:22,
                 from /usr/include/boost/asio/detail/deadline_timer_service.hpp:29,
                 from /usr/include/boost/asio/basic_deadline_timer.hpp:24,
                 from /usr/include/boost/asio.hpp:25,
                 from boost.cpp:1:
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:57:16: note: candidate: ‘template<class T> boost::posix_time::seconds::seconds(const T&, typename boost::enable_if<boost::is_integral<T>, void>::type*)’
   57 |       explicit seconds(T const& s,
      |                ^~~~~~~
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:57:16: note:   template argument deduction/substitution failed:
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp: In substitution of ‘template<class T> boost::posix_time::seconds::seconds(const T&, typename boost::enable_if<boost::is_integral<T>, void>::type*) [with T = double]’:
boost.cpp:19:59:   required from here
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:57:16: error: no type named ‘type’ in ‘struct boost::enable_if<boost::is_integral<double>, void>’
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note: candidate: ‘boost::posix_time::seconds::seconds(const boost::posix_time::seconds&)’
   53 |   class BOOST_SYMBOL_VISIBLE seconds : public time_duration
      |                              ^~~~~~~
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note:   no known conversion for argument 1 from ‘double’ to ‘const boost::posix_time::seconds&’
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note: candidate: ‘boost::posix_time::seconds::seconds(boost::posix_time::seconds&&)’
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note:   no known conversion for argument 1 from ‘double’ to ‘boost::posix_time::seconds&&’

What I can think of is the use of the line boost::posix_time::seconds(60.0) is not correct but I am not able to figure out what's the correct way to instantiate the posix_time::seconds class based on the above definitions. Do any of you have an idea about it?


Solution

  • That constructor takes an integral (whole) number. boost::posix_time::seconds(60) should work.

    Reference