Variadic templates with 'const' parameter overloading
c++, c++11, constants, variadic-templates
Solution
You just stumble upon the forwarding problem. This issue is solved using perfect forwarding.
Basically, you need to take your parameters by rvalue-reference, and rely on `std::forward` to correctly forward them while keeping their nature:
template<typename ...Args>
void proxy(Args&& ...args)
{
func(std::forward<Args>(args)...);
}
Problem
Reduced sample code: ``` #include <iostream> template<typename T> void func(T &x) { std::cout << "non-const " << x << std::endl; } template<typename T> void func(const T &x) { std::cout << "const " << x << std::endl; } template<typename ...ARGS> void proxy(ARGS ...args) { func(args...); } int main() { int i = 3; func(i); func(5); func("blah"); proxy(i); proxy(5); proxy("blah"); } ``` Expected output: ``` non-const 3 const 5 const blah non-const 3 const 5 const blah ``` Actual output: ``` non-const 3 const 5 const blah non-const 3 non-const 5 non-const blah ``` So somehow the `const` qualifier of the function parameter gets lost when put through the variadic template. Why? How can I prevent this? PS: tested with GCC 4.5.1 and SUSE 11.4