提升 protocol_type 命名空间

Boost protocol_type namespace

使用 C++11 工作让我觉得自己像个白痴。

我正在为 Boost Socket (boost::asio::ip::udp::socket) 编写一个简单的包装器 class。我包装了 Socket 的一个函数,open()。它需要 protocol_type。查看Boost headers,它的命名空间应该是boost::asio::。我包含 boost/asio/basic_socket.hpp 只是为了更好的衡量标准,因为它包含“protocol_type”的具体定义。

gcc 说“命名空间‘boost::asio’中的‘protocol_type’没有命名类型”。我已经尝试了几十个 headers 和更多的命名空间来解决它。这正是我所拥有的:

#include <boost/asio.hpp>
#include <boost/asio/basic_socket.hpp>

namespace sprocketa{

class BoostSocketWrapper {
public:
    /**
     * @brief Constructor that creates the Boost Socket
     *
     * @param ioService
     */
    BoostSocketWrapper(boost::asio::io_service& ioService);

    // with the exception of "virtual", this is the exact same signature as in the Boost Socket class
    virtual void open( const boost::asio::basic_socket::protocol_type & protocol = boost::asio::protocol_type() );

private:
    std::unique_ptr<boost::asio::ip::udp::socket> theSocket = nullptr;
};

}

有谁知道如何解决这个问题?

basic_socket 是一个 class 模板,它有两个参数,你必须定义它们:

template <typename Protocol, typename SocketService>
class basic_socket
  : public basic_io_object<SocketService>,
    public socket_base
{
public:
  /// The protocol type.
  typedef Protocol protocol_type;

protocol_type 是您作为第一个模板参数传递的内容,例如 boost::asio::ip::udp:

// with the exception of "virtual", this is the exact same signature as in the Boost Socket class
virtual void open( const boost::asio::basic_socket<boost::asio::ip::udp,
    boost::asio::stream_socket_service<boost::asio::ip::udp> >::protocol_type & protocol =
    boost::asio::ip::udp::v4() );