cpp-best-practices / cpp-best-practices/cppbestpractices
Const as Much as Possible (Not!)
- Lenguaje dominante
- Sin datos de lenguaje
- Estrellas
- 8.8k
- Forks
- 902
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
While "Const as Much as Possible" is generally good practice, `const` should not be directly used on function declaration parameters, including member functions.
A quick example:
```
void foo (int x);
void foo (int const x) {return x * 2;}
void bar (char const * s);
void bar (char const * const s) {puts(s);}
```
Notice I said "directly". Hence `s` isn't `const` in the declaration, but is in the definition (direct), but what `s` points at is `const` in both, and must match in both (indirect).
The reason for doing this is to not leak a function's implementation details. Because whether a function treats a parameter variable as `const` or not is an implementation detail of the function. And if a function gets modified such that its parameter is no longer treated as `const`, then callers of that function shouldn't have to be recompiled.
This is a difference between C and C++. In C, the function definition's parameter type(s) must exactly match that of the function declarations. (At least it used to. I haven't actually developed anything significant with modern C.)
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Línea de trabajo
En el issue no se nombra ningún archivo ni ninguna prueba. Empieza por localizar la sección de best practices que aborda los parámetros de función const y, después, compara sus indicaciones con los ejemplos de C y C++ del informe; se considera terminado cuando la documentación distingue claramente entre la constness directa de los parámetros y la constness de los datos apuntados.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- cpp
- Área
- documentation
- Tipo de issue
- Documentación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100