mercredi 27 avril 2016

std::forward with templated overloaded function

I've got no idea why compiler gives me warnings about template instantiations.

Thats a piece of code which runs just fine and outputs lvalue/rvalue properly:

//template<typename T>
void overloaded(const /*T*/std::string& in)
{
    std::cout << "lvalue" << std::endl;
}

//template<typename T>
void overloaded(/*T*/std::string&& in)
{
    std::cout << "rvalue" << std::endl;
}

template<typename T>
void pass(T&& in)
{
    overloaded(std::forward<T>(in));
}

int main()
{
    std::string a;
    pass(a);
    pass(std::move(a));
    getchar();
}

But i need to use it with templated type. So modifying the "overloaded" functions to

template<typename T>
void overloaded(const T& in)
{
    std::cout << "lvalue" << std::endl;
}

template<typename T>
void overloaded(T&& in)
{
    std::cout << "rvalue" << std::endl;
}

Gives template instantiations warnings, (when to me its clear T should be std::string), and console outputs rvalue 2 times instead of lvalue first.

What am i doing wrong?

Aucun commentaire:

Enregistrer un commentaire