Ethan Hoekstra
Ethan Hoekstra

Reputation: 7

std::bind c++ in if statement

I am working with some legacy code for a school project and am trying to capture if std::bind fails. This is the code currently in the project (not written by me) that is producing "C++ no operator matches these operands" error in VS 2019. I've tried comparing against a bool which is what it says std::bind returns to no avail.

if ( bind( socket, (const sockaddr*) &address, sizeof(sockaddr_in) ) < 0 )
{
    printf( "failed to bind socket\n" );
    Close();
    return false;
}

How else can I properly capture if std::bind fails within an if statement?

Upvotes: 0

Views: 410

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595896

The C++ std::bind() function does not return a value on failure, it throws an exception instead.

But the code you have shown is NOT trying to use std::bind() at all, it is actually trying to use the WinSock bind() function instead, but it can't because std::bind() is in scope due to a previous using namespace std; or using std::bind; statement (most likely the former), and so the compiler is trying to call std::bind() instead.

You need to either get rid of the using statement, or else prefix the bind() call with the :: global scope resolution operator:

if ( ::bind( socket, (const sockaddr*) &address, sizeof(sockaddr_in) ) < 0 )

Upvotes: 5

Related Questions