I am having trouble understanding how to forward the elements of a parameter pack in C++. Please take for example the code below:
#include <iostream>
void print(const int& value) {
std::cout << "print(const int& value) - " << value << std::endl;
}
void print(int&& value) {
std::cout << "print(int&& value) - " << value << std::endl;
}
template <class... Arg>
void print_each_argument(Arg&&... args) {
for(auto&& a: { args... }) {
print(std::forward<decltype(a)>(a));
}
}
int main() {
int some_value = 10;
int other_value = 20;
print_each_argument(some_value, other_value, 12, 14);
return 0;
}
Output:
print(const int& value) - 10
print(const int& value) - 20
print(const int& value) - 12
print(const int& value) - 14
What I expected to see was the following output:
Output:
print(const int& value) - 10
print(const int& value) - 20
print(int&& value) - 12
print(int&& value) - 14
Can someone explain on why the behavior is the one that it is?
I tried to compile the code and check the result.
const
, I'm surprised that you found a compiler that will compile this code. Is this the real code that's producing the output you show? – Ambages