【发布时间】:2011-02-13 21:18:30
【问题描述】:
在 c++ 应用程序中使用 boost 1.4.2 asio 并收到我不理解的 linux 编译器警告。
还在吗?
我正在开发的应用程序需要一个“套接字”,它可能是 ssl 套接字或常规 tcp 套接字,所以我们隐藏了细节
在采用 ssl 套接字类或
tcp 套接字类作为模板参数 - 下面是 ssl 类代码。
应用程序正常运行,未开启任何优化;问题是当我在 linux g++ 4.4.1 下编译时
并在 -02 或更高时打开优化,打开 -fstrict-aliasing 标志。
编译会导致严格的别名警告,如下所示:
“警告:取消引用类型双关指针将破坏严格的别名规则”
我在任何地方取消引用 _psock(例如 _psock->handshake)
我想知道为什么会发出这个警告,它是否表示设计问题...
class socket_ssl_cli
{
public:
typedef ba::ssl::stream<ba::ip::tcp::socket> socket_type;
socket_ssl_cli(ba::io_service& io_svc, SocketConfig & sockcfg)
: _io_svc(io_svc)
, _ctxt(_io_svc, ba::ssl::context::tlsv1)
, _psock(0)
{
try {
// the one and only ssl context
// hardcoded for test, but get these values from config
_ctxt.set_options(ba::ssl::context::default_workarounds |
ba::ssl::context::verify_none);
_psock = new socket_type(_io_svc, _ctxt);
} catch (bs::system_error & x) {
throw std::runtime_error(x.code().message());
}
}
~socket_ssl_cli()
{
if (_psock) {
bs::error_code ec;
close_socket(ec);
delete _psock;
}
}
socket_type & raw_socket() { return *_psock; }
void setup(bs::error_code & ec)
{
_psock->handshake(ba::ssl::stream_base::client, ec);
}
void close_socket(bs::error_code & ec)
{
// shut down ssl, then shutdown socket, then close socket
_psock->shutdown(ec);
_psock->lowest_layer().shutdown(ba::socket_base::shutdown_both, ec);
_psock->lowest_layer().close(ec);
}
private:
ba::io_service & _io_svc;
ba::ssl::context _ctxt;
socket_type * _psock;
};
所有痛苦的编译输出都打开了 -02,导致 -fstrict-aliasing
../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:321:警告:取消引用类型双关指针将破坏严格别名规则 ../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:325:警告:取消引用类型双关指针将破坏严格别名规则 ../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:在静态成员函数'static void boost::detail::function::functor_manager_common::manage_small (const boost::detail::function::function_buffer&, boost::detail::function::function_buffer&, boost::detail::function::functor_manager_operation_type) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >]': ../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:360: 从 'static void boost::detail::function::functor_manager::manager 实例化(const boost::detail::function::function_buffer&, boost::detail::function::function_buffer&, boost::detail::function::functor_manager_operation_type, mpl_::true_) [with Functor = boost::_bi:: bind_t > > >, boost::_bi::list1 > >*> > >]' ../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:406: 从 'static void boost::detail::function::functor_manager::manager 实例化(const boost::detail::function::function_buffer&, boost::detail::function::function_buffer&, boost::detail::function::functor_manager_operation_type, boost::detail::function::function_obj_tag) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >]' ../../../../third-party/boost/1.42.0/boost/function/function_base.hpp:434: 从 'static void boost::detail::function::functor_manager::manage 实例化(const boost::detail::function::function_buffer&, boost::detail::function::function_buffer&, boost::detail::function::functor_manager_operation_type) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >]' ../../../../third-party/boost/1.42.0/boost/function/function_template.hpp:913: 从 'void boost::function0::assign_to(Functor) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >, R = int]' ../../../../third-party/boost/1.42.0/boost/function/function_template.hpp:722:从 'boost::function0::function0(Functor, typename boost::enable_if_c ::type) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >, R = int]' ../../../../third-party/boost/1.42.0/boost/function/function_template.hpp:1064: 从'boost::function::function(Functor, typename boost::enable_if_c ::type) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >, R = int]' ../../../../third-party/boost/1.42.0/boost/function/function_template.hpp:1105: 从 'typename boost::enable_if_c&>::type boost::function:: 实例化operator=(Functor) [with Functor = boost::_bi::bind_t > > >, boost::_bi::list1 > >*> > >, R = int]' ../../../../third-party/boost/1.42.0/boost/asio/ssl/detail/openssl_operation.hpp:134:从'boost::asio::ssl::detail: :openssl_operation::openssl_operation(boost::asio::ssl::detail::ssl_primitive_func, Stream&, boost::asio::ssl::detail::net_buffer&, SSL*, BIO*) [with Stream = boost::asio ::basic_stream_socket >]' ../../../../third-party/boost/1.42.0/boost/asio/ssl/detail/openssl_stream_service.hpp:510:从'boost::system::error_code boost::asio实例化::ssl::detail::openssl_stream_service::handshake(boost::asio::ssl::detail::openssl_stream_service::impl_struct*&, Stream&, boost::asio::ssl::stream_base::handshake_type, boost: :system::error_code&) [with Stream = boost::asio::basic_stream_socket >]' ../../../../third-party/boost/1.42.0/boost/asio/ssl/stream_service.hpp:100:从'boost::system::error_code boost::asio::实例化ssl::stream_service::handshake(boost::asio::ssl::detail::openssl_stream_service::impl_struct*&, Stream&, boost::asio::ssl::stream_base::handshake_type, boost::system::error_code& ) [与 Stream = boost::asio::basic_stream_socket >]' ../../../../third-party/boost/1.42.0/boost/asio/ssl/stream.hpp:207:从 'boost::system::error_code boost::asio:: 实例化ssl::stream::handshake(boost::asio::ssl::stream_base::handshake_type, boost::system::error_code&) [with Stream = boost::asio::basic_stream_socket >, Service = boost::asio: :ssl::stream_service]' ../sockets/socket_ssl_cli.h:45:从这里实例化
【问题讨论】:
标签: c++ g++ boost-asio