I have a piece of code that can't compile well. I used variatic function and bind function together. The compiler error message show
error: no match for call to ‘(std::_Bind<std::_Mem_fn<void (SimpleList::*)(int&&)>(std::_Place holder<1>, int)>) (SimpleList&)’ slistInitter(simpleList);
#include <iostream>
#include <functional>
template <typename T>
std::reference_wrapper<T> maybe_wrap(T& val)
{
std::cout << "this is lvalue\n";
return std::ref(val);
}
template<typename T>
T&& maybe_wrap(T&& val)
{
std::cout << "this is rvalue\n";
return std::forward<T>(val);
}
void print(){}
template<typename T, typename... Args>
void print(const T& t, Args&&... args)
{
std::cout << t << std::endl;
print(std::forward<Args>(args)...);
}
class SimpleList
{
public:
template<typename... Args>
void init(Args&&... args)
{
std::cout << __FUNCTION__ << std::endl;
print(std::forward<Args>(args)...);
}
};
template<typename SLIST>
class Collection
{
public:
template<typename SLISTINITTER>
void initCollection(SLISTINITTER slistInitter)
{
std::cout << __FUNCTION__ << std::endl;
SLIST simpleList;
slistInitter(simpleList);
}
template<typename... Args>
void init(Args&&... args)
{
initCollection(std::bind(&SLIST::template init<Args...>, std::placeholders::_1, maybe_wrap(std::forward<Args>(args))...));
}
};
class test
{
public:
void work(const std::string& p1)
{
std::cout << p1 << std::endl;
}
test(){}
test(const test&)
{
std::cout << "this is copy\n";
}
test(test&& t)
{
std::cout << "this is move\n";
}
friend std::ostream& operator<<(std::ostream& os, const test& t);
};
std::ostream& operator<<(std::ostream& os, const test& t)
{
os << "cool";
return os;
}
int main()
{
using Sample = Collection<SimpleList>;
Sample s;
test t;
int i = 2;
s.init(2);
return 0;
}
If I used i as left value to pass to init function. It can compile well.
Aucun commentaire:
Enregistrer un commentaire