Why is c++ std::forward always return right value without template

66 Views Asked by At

I am trying to understand c++ perfect forwarding, and I do experiments with the following code:

#include <utility>
#include <iostream>

using namespace std;


void inner(int&) {cout << "left" << endl;}
void inner(int&&) {cout << "right" << endl;}

template<typename T>
void outer_templ(T&& t) {
    inner(std::forward<T>(t));
}

void outer(int& t) {
    inner(std::forward<int>(t));
}

void outer(int&& t) {
    inner(std::forward<int>(t));

}



int main() {
    int aa = 10;
    outer(aa);
    outer(10);
    outer_templ(aa);
    outer_templ(10);

    return 0;
}

And the result is:

right
right
left 
right

Does this mean that std::forward must be used together with template, or to be specific, std::forwarding must be used with universal reference?

0

There are 0 best solutions below