Quali sono le differenze tra modelli funzione con parametri di riferimento di inoltromodello di funzione abbreviato vs. modello di funzione con param riferimento inoltro
template<typename T>
void Universal_func(T && a)
{
}
e Modelli di Funzione abbreviati?
void auto_fun(auto && a)
{
}
Posso sostituire Universal_func
con auto_fun
? Universal_func
a auto_fun
o sono uguali?
Ho testato il seguente programma. Sembra che entrambi siano uguali.
template<typename T>
void Universal_func(T && a)
{
}
void auto_fun(auto && a)
{
}
int main()
{
int i;
const int const_i = 0;
const int const_ref =const_i;
//forwarding reference template function example
Universal_func(1); //call void Universal_func<int>(int&&)
Universal_func(i);//call void Universal_func<int&>(int&):
Universal_func(const_i); //call void Universal_func<int const&>(int const&)
Universal_func(const_ref);//call void Universal_func<int const&>(int const&)
//auto calls
auto_fun(1); //call void auto_fun<int>(int&&)
auto_fun(i);//call void auto_fun<int&>(int&):
auto_fun(const_i); //call void auto_fun<int const&>(int const&)
auto_fun(const_ref);//call void auto_fun<int const&>(int const&)
return 0;
}
Universal_func
e auto_fun
dedotto e ampliato per funzioni simili.
void Universal_func<int>(int&&):
pushq %rbp
movq %rsp, %rbp
movq %rdi, -8(%rbp)
nop
popq %rbp
ret
void Universal_func<int&>(int&):
pushq %rbp
movq %rsp, %rbp
movq %rdi, -8(%rbp)
nop
popq %rbp
ret
void Universal_func<int const&>(int const&):
pushq %rbp
movq %rsp, %rbp
movq %rdi, -8(%rbp)
nop
popq %rbp
ret
void auto_fun<int>(int&&):
pushq %rbp
movq %rsp, %rbp
movq %rdi, -8(%rbp)
nop
popq %rbp
ret
void auto_fun<int&>(int&):
pushq %rbp
movq %rsp, %rbp
movq %rdi, -8(%rbp)
nop
popq %rbp
ret
void auto_fun<int const&>(int const&):
pushq %rbp
movq %rsp, %rbp
movq %rdi, -8(%rbp)
nop
popq %rbp
ret
Ci sono differenze? Cosa dice lo standard?
la funzione con parametro auto non è standard C++. – 101010
@ 101010 È C++ 14. – Zereges
@Zereges No, non lo è. Lambdas può avere parametri 'auto' IIRC, ma le funzioni non sono ancora definitive. – hvd