Move to lambda capture C++11 - c++

In my code I'm using Item 32 advice from Scott Meyers Effective Modern C++, where he explains how to move into the capture in C++11. The sample code works well.
class Some
{
public:
void foo()
{
std::string s = "String";
std::function<void()> lambda = std::bind([this](std::string& s) {
bar(std::move(s));
}, std::move(s));
call(std::move(lambda));
}
void bar(std::string)
{
// Some code
}
void call(std::function<void()> func)
{
func();
}
};
int main()
{
Some().foo();
}
Then I tried to use move in the capture in more difficult way with parameters, but it doesn't work, there are some compilation errors. Please, help me to fix it. Code with error below. I've played with it, but couldn't find a solution. Is it possible to do it?
class Some
{
public:
void foo()
{
std::string stringToMove = "String";
std::function<void(std::string, int, int)> lambda =
std::bind([this](std::string s, int i1, int i2, std::string& stringToMove) {
bar(std::move(stringToMove), i1, i2);
}, std::move(stringToMove));
call(std::move(lambda));
}
void bar(std::string, int, int)
{
// Some code
}
void call(std::function<void(std::string, int, int)> func)
{
func(std::string(), 5, 5);
}
};
int main()
{
Some().foo();
}
Errors:
Severity Code Description Project File Line Suppression State
Error C2672 'std::invoke': no matching overloaded function
found drafts c:\program files (x86)\microsoft visual studio
14.0\vc\include\type_traits 1468
Severity Code Description Project File Line Suppression State
Error C2893 Failed to specialize function template 'unknown-type
std::invoke(_Callable &&,_Types &&...)' drafts c:\program files
(x86)\microsoft visual studio 14.0\vc\include\type_traits 1468

std::bind requires of you to specify all the parameters. So those which should be passed to the resulting function object, need to be placeholders.
std::function<void(std::string, int, int)> lambda =
std::bind([this](std::string s, int i1, int i2, std::string& stringToMove) {
bar(std::move(stringToMove), i1, i2);
}, _1, _2, _3, std::move(stringToMove));

Related

std::bind causes "failed to specialize function template" error on MSVC

As a bit of context on the issue I have, I am trying to compile my project using MSVC (Visual Studio 2022) on Windows and it produces a bunch of errors, which is surprising, considering the same code builds just fine on GNU-G++.
What I am trying to do is to write a packet spoofer using the libtins library. I am working on a multi-threaded approach, where one thread captures the packets and pushes them to a queue and the other pops out one packet at a time, does "the spoofing", and then forwards it somewhere else.
A construct is provided for capturing network packets in a loop, in the form of a template function.
/* Credits: M.Fontanini, libtins */
template <typename Functor>
void Tins::BaseSniffer::sniff_loop(Functor function, uint32_t max_packets) {
for(iterator it = begin(); it != end(); ++it) {
try {
// If the functor returns false, we're done
#if TINS_IS_CXX11 && !defined(_MSC_VER)
if (!Tins::Internals::invoke_loop_cb(function, *it)) {
return;
}
#else
if (!function(*it->pdu())) {
return;
}
#endif
}
catch(malformed_packet&) { }
catch(pdu_not_found&) { }
if (max_packets && --max_packets == 0) {
return;
}
}
}
This works by binding a callback function which will be called every time a packet is captured. I have tried to leverage this by creating a wrapper class, called PacketSniffer, where I bind a callback function to Sniffer::sniff_loop which pushes every captured packet to a queue.
bool
PacketSniffer::callback(Tins::Packet &packet,
ThreadSafeQueue<Tins::Packet> &packetq,
bool &running) {
packetq.push(packet);
return running;
}
void PacketSniffer::run(ThreadSafeQueue<Tins::Packet> &packetq, bool &running) {
try {
sniffer_->sniff_loop(std::bind(&PacketSniffer::callback, this,
std::placeholders::_1, std::ref(packetq),
std::ref(running)));
} catch (std::exception &ex) {
throw std::runtime_error(ex.what());
}
}
The actual call where I use this in my app:
// Packet capture
bool running = true;
std::thread capture([&pq = packetq_, st, &running,
&iface_value, &pcap_filter_value]() {
PacketSniffer ps(st, iface_value.data(),
pcap_filter_value.data());
ps.run(pq, running);
});
MSVC compiler error:
C:\Users\adrian\repos\src\spoofer\build\_deps\libtins-src\include\tins/sniffer.h(681,18): error C2672: 'operator __surrogate_func': no matchi
ng overloaded function found [C:\Users\adrian\repos\src\spoofer\build\src\spoofer.vcxproj]
C:\Users\adrian\repos\src\spoofer\src\sniffer.cpp(74): message : see reference to function template instantiation 'void Tins::BaseSniffer::sn
iff_loop<std::_Binder<std::_Unforced,bool (__cdecl spoofer::PacketSniffer::* )(Tins::Packet &,ThreadSafeQueue<Tins::Packet> &,bool &),spoofer::
PacketSniffer *,const std::_Ph<1> &,std::reference_wrapper<ThreadSafeQueue<Tins::Packet>>,std::reference_wrapper<bool>>>(Functor,uint32_t)' b
eing compiled [C:\Users\adrian\repos\src\spoofer\build\src\spoofer.vcxproj]
with
[
Functor=std::_Binder<std::_Unforced,bool (__cdecl spoofer::PacketSniffer::* )(Tins::Packet &,ThreadSafeQueue<Tins::Packet> &,boo
l &),spoofer::PacketSniffer *,const std::_Ph<1> &,std::reference_wrapper<ThreadSafeQueue<Tins::Packet>>,std::reference_wrapper<bool>>
]
C:\Users\adrian\repos\src\spoofer\build\_deps\libtins-src\include\tins/sniffer.h(681,1): error C2893: Failed to specialize function template
'unknown-type std::_Binder<std::_Unforced,bool (__cdecl spoofer::PacketSniffer::* )(Tins::Packet &,ThreadSafeQueue<Tins::Packet> &,bool &),spo
ofy::PacketSniffer *,const std::_Ph<1> &,std::reference_wrapper<ThreadSafeQueue<Tins::Packet>>,std::reference_wrapper<bool>>::operator ()(_Un
bound &&...) noexcept(<expr>) const' [C:\Users\adrian\repos\src\spoofer\build\src\spoofer.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\functional(2002): message : see declaration of 'std
::_Binder<std::_Unforced,bool (__cdecl spoofer::PacketSniffer::* )(Tins::Packet &,ThreadSafeQueue<Tins::Packet> &,bool &),spoofer::PacketSniffe
r *,const std::_Ph<1> &,std::reference_wrapper<ThreadSafeQueue<Tins::Packet>>,std::reference_wrapper<bool>>::operator ()' [C:\Users\adrian\r
epos\src\spoofer\build\src\spoofer.vcxproj]
C:\Users\adrian\repos\src\spoofer\build\_deps\libtins-src\include\tins/sniffer.h(681,1): message : With the following template arguments: [C:
\Users\adrian\repos\src\spoofer\build\src\spoofer.vcxproj]
C:\Users\adrian\repos\src\spoofer\build\_deps\libtins-src\include\tins/sniffer.h(681,1): message : '_Unbound={Tins::PDU &}' [C:\Users\adrian
\repos\src\spoofer\build\src\spoofer.vcxproj]
A minimal, single threaded example from one of my tests, producing the same error. Don't know how useful this is, as it needs to be compiled and linked against libtins, but maybe it can provide some additional context:
#include <memory>
#include <iostream>
#include <exception>
#include <functional>
#include <tins/tins.h>
#include "utils/queue.h"
enum class SnifferType { Sniffer, FileSniffer };
class PacketSniffer {
public:
PacketSniffer(SnifferType st, const char *iface, const char *capture_filter) {
setup(st, iface, capture_filter);
}
PacketSniffer() = delete;
void run(ThreadSafeQueue<Tins::Packet>& packetq, bool &running);
private:
void setup(SnifferType st, const char *iface, const char *capture_filter) {
Tins::SnifferConfiguration config;
config.set_promisc_mode(true);
config.set_filter(capture_filter);
try {
if (st == SnifferType::FileSniffer) {
sniffer_ = std::make_unique<Tins::FileSniffer>(iface, config);
} else {
sniffer_ = std::make_unique<Tins::Sniffer>(iface, config);
}
} catch (Tins::pcap_error &e) {
throw std::runtime_error(e.what());
} catch (std::exception &e) {
throw std::runtime_error(e.what());
}
}
bool callback(Tins::Packet& packet,
ThreadSafeQueue<Tins::Packet>& packetq,
bool &running){
packetq.push(packet);
return running;
}
std::unique_ptr<Tins::BaseSniffer> sniffer_;
};
struct TestContext {
TestContext(const char *file_path, const char *filter) :
sniffer_({ SnifferType::FileSniffer, file_path, filter}) {}
PacketSniffer sniffer_;
ThreadSafeQueue<Tins::Packet> queue_;
};
int main() {
TestContext ctx("packets.pcap", "");
bool running = true;
ctx.sniffer_.run(ctx.queue_, running);
return 0;
}
What am I missing here regarding std::bind, that produces these errors? I find it weird that the code compiles on G++ but not on MSVC and I think it's related to this somehow.

VS 2015 Error when using nested lambda

Here is a toned down version of my use case which compiles fine on gcc 4.9.3 and clang 3.7 but fails on VS 2015 Update 2.
#include <functional>
#include <string>
#include<memory>
namespace A {
using handle = std::function<void(std::string)>;
void func(std::string, handle) {
}
}
class B : public std::enable_shared_from_this<B> {
public:
void func();
};
void B::func()
{
auto && cast = std::static_pointer_cast<B>(shared_from_this());
auto const another_variable = [cast]() -> void
{
A::func("hello", [&cast](std::string){ } );
};
}
int main() {
return 0;
}
Here are the errors that I get
Error(s):
source_file.cpp(23): error C2440: '<function-style-cast>': cannot convert from 'const std::shared_ptr<_Ty>' to 'B::func::<lambda_4a58826445f3685613b078cf10fc9f7e>::()::<lambda_8290a9c207487c2dc2e26a5b929b4850>'
with
[
_Ty=B
]
source_file.cpp(23): note: No constructor could take the source type, or constructor overload resolution was ambiguous
source_file.cpp(23): error C2660: 'A::func': function does not take 1 arguments
Interestingly, it compiles fine on VS 2013 as well. Am I missing something very obvious?

Passing template argument to template function in a thread

I'm new to templates, so I decided to write out unit tests for some concurrent code I am writing, but I can't seem to get them to compile. The specific error is:
error C2664: 'std::thread::thread(const std::thread &)' : cannot convert argument 1 from 'void (__cdecl *)(lock &)' to 'void (__cdecl &)(Utility_UnitTests::emptyLock &)'
1> None of the functions with this name in scope match the target type
1> w:\code dumpster\utility_unittests\utspinlock.cpp(88) : see reference to function template instantiation 'void Utility_UnitTests::UTSpinLock::lockContension<Utility_UnitTests::emptyLock>(lock &)' being compiled
1> with
1> [
1> lock=Utility_UnitTests::emptyLock
1> ]
The issue is pretty clear from the compiler, I am not passing the correct type, but I have no clue how to fix it! Thanks in advance!
EDIT: I forgot to mention, I am using Visual Studio 2013
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Utility_UnitTests
{
typedef utils::threading::SpinLock<utils::threading::backoff::empty> emptyLock;
typedef utils::threading::SpinLock<utils::threading::backoff::yield> yieldingLock;
typedef utils::threading::SpinLock<utils::threading::backoff::pause> pausingLock;
TEST_CLASS(UTSpinLock)
{
public:
template<typename lock>
void lockAndSleepT(lock &l)
{
l.lock();
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
l.unlock();
}
template<typename lock>
void lockContension(lock &l)
{
std::thread t1(&UTSpinLock::lockAndSleepT<lock>, this, std::ref(l));
Assert::AreEqual(true, l.isLocked());
t1.join();
Assert::AreEqual(false, l.isLocked());
}
TEST_METHOD(testLockContension)
{
UTSpinLock::lockContension(m_emptySpin);
UTSpinLock::lockContension(m_yieldingSpin);
UTSpinLock::lockContension(m_pausingSpin);
}
private:
emptyLock m_emptySpin;
yieldingLock m_yieldingSpin;
pausingLock m_pausingSpin;
};
}
First of all, this is certainly a bug in the MSVC implementation. It seems to be having trouble when the first argument to std::thread is a pointer to a member function template. On my machine, the 64-bit compiler produces the same error message, while the 32-bit compiler crashes. Thankfully, you can work around this in quite a few ways, all involving not directly pass a pointer to a member function template to thread.
Option 1 - as you've discovered, creating a bind expression and passing that to thread works.
Option 2 - rewrite the class so that it is a template, and the member functions are not.
template<typename lock>
struct UTSpinLock
{
public:
void lockAndSleepT(lock &l)
{}
void lockContension(lock &l)
{
std::thread t1(&UTSpinLock::lockAndSleepT, this, std::ref(l));
t1.join();
}
};
Option 3 - leave the class definition unchanged, and wrap the pointer to member function template in std::mem_fn
std::thread t1(std::mem_fn(&UTSpinLock::lockAndSleepT<lock>), this, std::ref(l));
Option 4 - no change to the class definition again, this time pass a lambda expression to thread
std::thread t1([&, this](){ lockAndSleepT(l); });
Special thank to WhozCraig! I would accept his answer, but, for some reason, I can't. For those that are interested, I changed:
template<typename lock>
void lockContension(lock &l)
{
std::thread t1(&UTSpinLock::lockAndSleepT<lock>, this, std::ref(l));
Assert::AreEqual(true, l.isLocked());
t1.join();
Assert::AreEqual(false, l.isLocked());
}
To:
template<typename lock>
void lockContension(lock &l)
{
auto bounded = std::bind(&UTSpinLock::lockAndSleepT<lock>, this, std::placeholders::_1);
std::thread t1(bounded, std::ref(l));
Assert::AreEqual(true, l.isLocked());
t1.join();
Assert::AreEqual(false, l.isLocked());
}

Visual C++ function and bind problems

I have a class which works with files, the constructor is receiving a std::function for sending the status of the work to a GUI Library if used.
Declaration:
DASM(std::string filename, std::function<void(unsigned int, unsigned int)> f = nullptr);
in the definition I have wrote:
if(f!=nullptr)
f(i,decodedInstructionsCount);
my LoadWindow has following function:
std::function<void(unsigned int,unsigned int)> get_f()
{
std::function<void(unsigned int,unsigned int)> ret = std::bind(&LoadWindow::pb_next,_2,_1);
return ret;
}
and the void LoadWindow::pb_next looks like this:
void LoadWindow::pb_next(unsigned int now, unsigned int max)
{
this->m_progressbar.set_fraction(((double)now/(double)max));
}
VC++ says:
error C2064: Expression does not resolve in a function which accepts 2 arguements.
I have a German VS so I translated the error.
Thank you for Help

std::bind2nd on an already bind1st std::function

I am trying to figure out how binders work. I am working on the example from HERE. So I decided to extend it a bit but I can't figure out what is wrong
namespace mine {
using std::bind1st;
using std::bind2nd;
using std::function;
};
struct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { std::cout << num_+i << '\n'; }
int num_;
};
int main()
{
using namespace mine;
// store a call to a member function
function<void(const Foo&, int)> f_add_display = &Foo::print_add;
Foo foo(314159);
f_add_display(foo, 1);
function<void(int)> foo_f_add_display = bind1st(f_add_display, foo);
foo_f_add_display(1);
// The problem is here <------------
function<void(const Foo&)> f_1_add_display = bind2nd(f_add_display, 1);
// f_1_add_display(foo);
}
The error message I am getting (from Intel CC, gdb is unintelligible)
Compilation finished with errors:
c++/4.7/backward/binders.h(160): error: invalid redeclaration of member function "std::binder2nd<_Operation>::operator()(const _Operation::first_argument_type &) const [with _Operation=std::function<void (const Foo &, int)>]" (declared at line 154)
operator()(typename _Operation::first_argument_type& __x) const
^
detected during instantiation of class "std::binder2nd<_Operation> [with _Operation=std::function<void (const Foo &, int)>]" at line 41 of "source.cpp"
compilation aborted for source.cpp (code 2)
What exactly is the problem here. Why is not possible to bind the second argument? or is it just some syntax error or something?
Code is HERE if anyone needs it.