I have a construct in a form:
template<class T>
void do_something_with_it(T*&& ptr)
{
//here I can do something with this ptr
}
template<class T,class... Args>
void do_something_with_them(T*&& ptr, Args&&... args)
{
do_something_with_it(std::forward<T&&>(ptr));
do_something_with_them(std::forward<Args&&>(args)...);
}
but for some reason I cannot forward those arguments. Is there a way to do it?
I’m using gcc 4.6.1.
Chances are that you get compile time errors, because
T*&&is not a perfect forwarding vehicle. OnlyT&&is. So yourptrparameter only accepts rvalues. And in addition to that, yourstd::forward<T&&>should bestd::forward<T*>, but of course now that you have the other error anyway this is irrelevant. And in addition to that the call todo_something_with_themmisses to hit a base case ofdo_something_with_themwith zero parameters, because ifargsis empty…If you really only want to accepts pointers, you can work with
enable_ifandis_sameoris_convertible. But then of course I don’t think it’s “forwarding” anymore. What aboutThat way you let
do_something_with_itdecide whether or not it accepts the argument (if you want you can put the recursive call into thatdecltypetoo. I leave it as an exercise to the reader as to what operator might be needed here). But of coursedo_something_with_ithas the same problem too about not being generic.