1
votes

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?

1
Have you tried std::shared_ptr<CParameterInfo> info(new CParameterInfo());? Or even better, auto info = std::make_shared<CParameterInfo>();? - Kerrek SB
I guess the problem is the fact that std::shared_ptr has constructors that are both templates and no templates and you can't do anything about that. If I understand it correctly, IMHO the rule is too strict. - Cassio Neri
@CassioNeri your probably right it seems to be a problem in the implementation of shared_ptr, and that it has a template and function with the same signature, making PC-Lint to show the MISRA error. Bur it works when using make_shared so I'll do that. - MathiasWestin

1 Answers

4
votes

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.