¿Cuál es la diferencia entre auto y decltype (auto) al regresar de una función?
Rara vez veo decltype(auto)
pero cuando lo hago me confunde porque parece hacer lo mismo que auto
al regresar de una función.
auto g() { return expr; }
decltype(auto) g() { return expr; }
¿Cuál es la diferencia entre estas dos sintaxis?
35
Author: Nicol Bolas, 2014-01-27
2 answers
auto
sigue las reglas de deducción del argumento de plantilla y siempre es un tipo de objeto; decltype(auto)
sigue las reglas decltype
para deducir tipos de referencia basados en categorías de valor. Así que si tenemos
int x;
int && f();
Entonces
expression auto decltype(auto)
----------------------------------------
10 int int
x int int
(x) int int &
f() int int &&
41
Author: Kerrek SB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-08 19:05:30
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-08 19:05:30
auto
devuelve qué tipo de valor se deduciría de la cláusula return
asignada a una variable auto
. decltype(auto)
devuelve el tipo que obtendría si envolviera la cláusula return en decltype
.
auto
devuelve por valor, decltype
tal vez no.
8
Author: Yakk - Adam Nevraumont,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-26 21:05:33
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-26 21:05:33