0

I've got code that compiles with Visual Studio 2017 that uses std::bind:

std::unique_lock<std::mutex>    m_lock(m_mutex_wait_for_message);
m_cond_variable.wait(m_lock, std::bind(&Logging::is_message_available, this));
std::lock_guard<std::mutex>     lock_guard(m_mutex_pushing_message);

We are now compiling using VS2019 and it's complaining with errors:
'bind': is not a member of 'std'
'bind': function does not take 2 arguments

CppReference.com says "Until C++20"

Questions:

  1. What is the replacement for std::bind in the mutex locking code above?
  2. What is the replacement for std::bind?
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 1
    `std::bind` should work just fine. Did you `#include `? Anyway, you are probably looking for `m_cond_variable.wait(m_lock, [this]() { return is_message_available(); });` – Igor Tandetnik Sep 21 '22 at 00:26
  • 2
    It only says "until C++20" because there is a new declaration including `constexpr` under it marked "since C++20". – user17732522 Sep 21 '22 at 02:18
  • 1
    ` is not a member of 'std` is the error message you get when you don't include the header file – Tom Huntington Sep 21 '22 at 03:46

1 Answers1

0

As others have mentioned, you're likely missing the header file that includes it. To address your other points:

When cppreference says until C++20, note that immediately underneath it says since C++20, meaning that the signature of the function changed in C++20, and it is showing both before and after. In this case, constexpr was added to the function, std::bind is still there.

As for what replaces std::bind, since C++14 you can use a lambda in place of every instance of std::bind, see here for a better explanation. Applying this to your code you can write:

m_cond_variable.wait(m_lock, [&] { is_message_available(); });

In C++20 you can leave off the () for lambdas that take no arguments.

Matt Cummins
  • 309
  • 5