Suppose that I have two functions below, in the Foo() function, how can I pack the hw string into args and forward them to Bar()?
I tried std::bind but that didn't work.
template<typename T, typename... Args>
void Bar(Args&&... args)
{
// do something with args
}
template<typename T, typename... Args>
void Foo(Args&&... args)
{
if (typeid(T) == typeid(std::string)) {
std::string hw = "Hello, world!";
Bar<T>(std::forward<Args>(hw, args)...); // how to add hw to the forward list?
}
else {
Bar<T>(std::forward<Args>(args)...);
}
}
Edit: Finally I found my bug! For those of you who's wondering why hw doesn't get forwarded to Bar() even if you did it right, please pay attention to the Bar() in the else branch. If Bar() expects different types of arguments depending on T and the code won't compile, the compiler errors might be emitted by the else branch. As @JeJo mentioned, I should have used if constexpr instead.
You may find this post helpful: using std::is_same, why my function still can't work for 2 types