mardi 4 août 2015

forcing template type to be reference by deduction

Say I have a function func:

template<typename T>
auto func(T arg){
    std::cout << std::boolalpha;
    std::cout << "T is ref: " << std::is_reference<T>::value << '\n';
}

Is there a way I can force T to be deduced as a reference type without explicitly specifying the template parameters?

Like being able to write something like:

auto main() -> int{
    auto x = 5;
    func(std::ref(x));
}

but without having to specialize for std::reference_wrapper.

static_casting does not stop the decay of int& into int for T.

Imagine that I can not change the function signature.

Aucun commentaire:

Enregistrer un commentaire