1

I am getting warning #12367 when running Build | Build Solution for Intel Static Analysis, but I can't see the problem with my code. Ideas anyone?

warning #12367: slicing of object passed as actual argument 2 in call "std::_Vector_iterator > > std::vector >::erase(std::_Vector_const_iterator > >)" occurs due to implicit type conversion

 pragma warning(suppress: 4995)
    #include <vector>

    class __declspec(dllexport) MxPluginLib //nothing special here, not derived class etc
    {
    public:
      // ...
    private:
     //  ... nothing special here

    };


    class __declspec(dllexport) MxPluginManager 
    {
    public:
       //...
    private:
    #pragma warning(suppress: 4251)
        std::vector<MxPluginLib *> _Libs;

    };

    bool MxPluginManager::DeleteNextUnselected()
    {
        bool erased = false;
        size_t cnt = _Libs.size();
        if (cnt > 0 )
        {
            for (size_t x = 0; x < cnt; x++)
            {
                if (_Libs[x]->GetSelection() == false)
                {
                    delete  _Libs[x];
                    _Libs[x] = '\0';
                    _Libs.erase(_Libs.begin()+x);  //THIS IS WHERE THE WARNING IS GENERATED
                    erased = true;
                    break;
                }
            }
        }
        return erased;
    }
Massimiliano
  • 16,770
  • 10
  • 69
  • 112
wpqs
  • 657
  • 1
  • 7
  • 18

2 Answers2

0

In my view the only weird line is

_Libs[x] = '\0';

It might make the analyzer think that you're storing chars instead of pointers. Since you're erasing the element anyway you don't really need it -- remove it, and see if it solves your problem.

Anton Belov
  • 534
  • 4
  • 10
0

This line

_Libs.erase(_Libs.begin()+x);

is passing a vector::iterator to a function expecting a vector::const_iterator. This is ok, and required to work (but the standard doesn't say exactly how it works, just that an iterator can be converted to a const_iterator).

On your implementation, it seems like iterator is derived from const_iterator and passing the parameter will convert it to the base class by "slicing" off the derived part.

In this case this is not a error, and the warning can be ignored.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203