vendredi 1 février 2019

Why does universal reference not keep constness of its arguments?

template<typename T>
void f(T&& n)
{
    ++n; // ok to modify a const object, why?
}

template<typename T>
void g()
{
    int n{};
    f<const T&>(n);
}

int main()
{
    g<int&>();
}

As shown in the code above. My question is:

Why does universal reference not keep constness of its arguments?

Aucun commentaire:

Enregistrer un commentaire