C++ Contradicting rules cpp:S6011 and cpp:6003

Template for a good new topic, formatted with Markdown:

  • ALM used: Azure DevOps
  • CI system used: Azure DevOps
  • Scanner command used when applicable (private details masked)
    Azure Pipeline task 4.2.3 with build wrapper
                & $msbuild MySolution.sln /t:restore
                & "$(BuildWrapper)" --out-dir "$(SonarOutDir)" `
                  "$msbuild" MySolution.sln `
                  /t:Rebuild /p:Configuration=$(BuildConfiguration) /nodeReuse:False /nologo
  • Languages of the repository: C++, C#
  • Error observed (wrap logs/code around with triple quotes ``` for proper formatting)
std::vector<std::array<double, 3>>& colors;

...

ResultArray<double> doublesResult; // that is a wrapper arround "T* Data" for an external C library

 ...

colors.push_back({doublesResult.Data[0], doublesResult.Data[1], doublesResult.Data[2]}); // compiles, result in cpp:S6003
colors.emplace_back(std::array<double, 3>{doublesResult.Data[0], doublesResult.Data[1], doublesResult.Data[2]}); // compiles, result in cpp:S6011
//colors.emplace_back(doublesResult.Data[0], doublesResult.Data[1], doublesResult.Data[2]); // does not compile C2672
//colors.emplace_back({doublesResult.Data[0], doublesResult.Data[1], doublesResult.Data[2]}); // does not compile C2672
//colors.emplace_back(doublesResult.Data); // does not compile, C2672

Hello @milbrandt,

You are right that there is not really any better way to write this code than the first version with push_back. We should not raise an issue here.

The problem comes from the fact that initializing a std::array relies on aggregate initialization, which is not fully supported in emplace_back.

In versions of C++ before C++20, it was not supported at all, so we would not raise an issue (please, can you confirm that your code targets a version of C++ greater or equal to C+20?).

Since C++20, it is partially supported, but still not for std::array (you can read this blog post for more details), and we don’t currently detect this case.

I created a ticket to improve this behavior. Thank you for reporting it.

Hi @JolyLoic

I confirm, we are targeting stdcpp23 and using MSVC 14.51 (latest Visual Studio 2026).
The snippet was from an unit test.