cpp-best-practices / cpp-best-practices/cppbestpractices

Const as Much as Possible (Not!)

Aperta
#128 3 commenti 3 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Nessun dato sulla lingua
Stelle
8.8k
Fork
902
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.)

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Nell’issue non sono indicati file né test. Inizia individuando la sezione best practices che tratta i parametri di funzione const, quindi confronta le relative indicazioni con gli esempi in C e C++ riportati nel report; il lavoro è completato quando la documentazione distingue chiaramente la constness diretta dei parametri dalla constness dei dati puntati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
cpp
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.