Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:13:57+00:00 2026-06-05T14:13:57+00:00

boost::asio::ip::tcp::socket& socket() const is returning _socket which is of type boost::asio::ip::tcp::socket is giving me

  • 0

boost::asio::ip::tcp::socket& socket() const is returning _socket which is of type boost::asio::ip::tcp::socket is giving me this error

error: invalid initialization of non-const reference of type ‘boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >&’ from a temporary of type ‘const boost::reference_wrapper<const boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >’

socket() function is only getter and is not altering the state of this object so why does it fire error if I make it const ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:13:59+00:00Added an answer on June 5, 2026 at 2:13 pm

    Because the function is returning a reference to an object. When the const member function is called, the returned reference must also be const.

    boost::asio::ip::tcp::socket const& socket() const

    However, depending on what you want to do with that socket reference, you might have to return a non-const reference, in which case another option is to declare the getter function as non-const or declare the socket mutable in the class definition

    mutable boost::asio::ip::tcp::socket socket_

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code: tcp::socket socket(io_service); tcp::endpoint ep(boost::asio::ip::address::from_string(addr), i); socket.async_connect(ep, &connect_handler); socket.close(); is it
Alright, this is my current code snippet: namespace bai = boost::asio::ip; bai::tcp::socket tcp_connect(std::string hostname,
I'm trying to implement a timeout for a Boost.Asio read on a TCP socket.
I'm using boost::asio, and I have code like this: void CServer::Start(int port) { tcp::acceptor
Unlike this question: Linker Error while building application using Boost Asio in Visual Studio
I wrote a small tcp client using boost::asio, providing the following function: typedef boost::function<void(const
My client application uses a boost::asio::ip::tcp::socket to connect to a remote server. If the
I can't figure out why I get this error : /usr/local/include/boost/asio/impl/read.hpp: In member function
I'm trying to code a wrapper over a boost::asio::ip::tcp::socket Something like that : class
I have some socket connection code that makes use of boost::asio which reads from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.