How to create a std::shared_ptr without violating MISRA C++ 2008 Advisory Rule 14-8-2?

StackOverflow https://stackoverflow.com/questions/17996121

  •  04-06-2022
  •  | 
  •  

I get this error in PC-Lint (au-misra-cpp.lnt):

ParameterTest.cpp(40): error 1963: (Note -- Violates MISRA C++ 2008 Advisory Rule 14-8-2, Viable set contains both function and template: std::shared_ptr::shared_ptr (line 499, file C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory, module Parameter.cpp), std::shared_ptr::shared_ptr (line 485, file C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory, module Parameter.cpp)) C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory(499): error 830: (Info -- Location cited in prior message) std::shared_ptr info(infoPtr);

On this code:

CParameterInfo* infoPtr = new CParameterInfo();
std::shared_ptr<CParameterInfo> info(infoPtr);

I've tried to write the code in different ways, but can't find a way that don't give the error above.

Is it possible to make the code MISRA compliant?

有帮助吗?

解决方案

I'd guess that avoiding the constructor might work:

auto infoPtr = std::make_shared<CParameterInfo>();

This also has the advantage of only doing a single memory allocation, while separate creation of the object and the shared state would need two.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top