MISRA Discussion Forums
Rule 11.6.1 - Variables initialized via output-parameter? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188)
+---- Forum: 4.11 Declarators (https://forum.misra.org.uk/forumdisplay.php?fid=197)
+---- Thread: Rule 11.6.1 - Variables initialized via output-parameter? (/showthread.php?tid=1748)



Rule 11.6.1 - Variables initialized via output-parameter? - cgpzs - 02-09-2025

Hi,

Consider the following example:

Code:
// external_c_api.h
enum error_t
{
  // ...
};

enum foo_t
{
  foo_A = 1,
  foo_B = 2
};

error_t externalCApi(foo_t*);

// main.cpp
void f()
{
  foo_t x;
  error_t error = externalCApi(&x);
}

How would one apply 11.6.1 in this case? It's not possible to avoid the two-step initialization process. There may not even be a "sensible default" to give to `x` before giving it the "real" value (and MISRA explicitly discourages this pattern). In particular, doing "foo_t x{};" leads to zero initialization, but there is no enumerator with the value 0.

Thanks!