Sintaxis de especialización de plantillas C++
En C++ Primer Plus (2001, Traducción checa) he encontrado estas sintaxis de especialización de plantillas diferentes:
Plantilla de Función
template <typename T> void foo(T);
Sintaxis de especialización
void foo(int param); // 1
void foo<int>(int param); // 2
template <> void foo<int>(int param); // 3
template <> void foo(int param); // 4
template void foo(int param); // 5
Buscando un poco en Google, solo he encontrado el número 3 ejemplos. ¿Hay alguna diferencia (en la llamada, compilación, uso) entre ellos? ¿Algunos de ellos son obsoletos/obsoletos? ¿Por qué no usar el número 1?
43
2 answers
Aquí hay comentarios con cada sintaxis:
void foo(int param); //not a specialization, it is an overload
void foo<int>(int param); //ill-formed
//this form always works
template <> void foo<int>(int param); //explicit specialization
//same as above, but works only if template argument deduction is possible!
template <> void foo(int param); //explicit specialization
//same as above, but works only if template argument deduction is possible!
template void foo(int param); //explicit instantiation
Añadido por mí:
//Notice <int>. This form always works!
template void foo<int>(int param); //explicit instantiation
//Notice <>. works only if template argument deduction is possible!
template void foo<>(int param); //explicit instantiation
Desde el punto de vista de la codificación, se prefiere la sobrecarga sobre la especialización de la plantilla de función.
Entonces, no especialices la plantilla de función:
Y conocer las terminologías:
- instanciación
- explícito instanciación
- especialización
- especialización explícita
Vea esto:
68
Author: Nawaz,
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-05-23 11:54:44
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-05-23 11:54:44
Usando Visual Studio 2012, parece funcionar ligeramente diferente si no hay un argumento de función:
template <typename T> T bar( );
//template int bar<int>( ) { return 0; } doesn't work
template < > int bar<int>( ) { return 0; } //does work
1
Author: gerardw,
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-03 17:32:14
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-03 17:32:14