lundi 9 octobre 2017

Including std::forward yield an error, but its omission compiles. Why?

I think I'm fundamentally misunderstanding something about std::forward. Take a look at the following code - if I include std::forward, it won't compile, but if it's omitted, it compiles fine. Isn't the point of std::forward that it can deduce if it's needed to push a l or rvalue?

#include "boost/optional.hpp"
#include "iostream"
template<typename T>
bool hasArg(const boost::optional<T>& opts)
{
    if (opts)
    {
        return true;
    }
    else
    {
        return false;
    }
}


template<typename T, typename ... Ts>
bool hasArg(const boost::optional<T> & opts,const boost::optional<Ts> & ... rest)
{
    if (opts) {
        //doesn't work: return hasArg<Ts...>(std::forward<Ts>(rest)...);
        //works: return hasArg<Ts...>(rest...);
        return hasArg<Ts...>(rest...);
    }
    else
    {
        return false;
    }
}


int main()
{
    const boost::optional<int> p = boost::optional<int>(5);
    const boost::optional<int> q = boost::optional<int>(6);
    std::cout << hasArg(p, q) << std::endl;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire