Here i have small piece of code and it compiles and works just fine (at least with my GCC 7.3.0 and Ubuntu 18.04):
#include <functional>
#include <string>
#include <iostream>
void func(int a, const std::string& b, const std::string& c)
{
std::cout << a << b << c << std::endl;
}
class Test
{
public:
template <typename ... ARGS>
bool func_to_bind(ARGS&& ... args) const {
func(args...);
return true;
}
template <typename ... ARGS>
void binding_func(ARGS&& ... args) const
{
auto func_obj = std::bind(&Test::func_to_bind<int&, ARGS&...>, this, 42, args...);
func_obj();
}
};
int main()
{
Test obj;
obj.binding_func(std::string("one"), std::string("two"));
}
The part that i don't understand is this line:
std::bind(&Test::func_to_bind<int&, ARGS&...>, this, 42, args...);
Why does compiler require to use references as template type parameters? If i remove reference from int like this:
std::bind(&Test::func_to_bind<int, ARGS&...>, this, 42, args...);
It won't compile. Also if i change func_to_bind signature to this:
bool func_to_bind(ARGS& ... args) const
It will compile just fine even with missing reference. Could anyone explain what's exactly going on here? I also did some search and found this question: How to combine std::bind(), variadic templates, and perfect forwarding?
But i don't completely understand the answer.