Search code examples
c++visual-c++comatlsafearray

Why can't I Add 0 to CComSafeArray?


CComSafeArray<BYTE> arr;
arr.Add(0x00);

Error -> C2668: 'ATL::CComSafeArray::Add' : ambiguous call to overloaded function

I can add any value but can't add 0 , why?

btw currently I'm doing

const byte zero = 0x00;
arr.Add(zero);

but I don't understand the reason why I can't just add 0


Solution

  • The method adding an element is :

    HRESULT Add(_In_ const T& t, _In_ BOOL bCopy = TRUE) // 0x00 as zero for t
    

    that is, your argument is expected to be of type const BYTE&. There is however another Add method which can accept your zero argument:

    HRESULT Add(_In_ const SAFEARRAY *psaSrc) // 0x00 as NULL for pasSrc
    

    Hence the ambiguity and you are expected to resolve it by casting your argument appropriately:

    CComSafeArray<BYTE> arr;
    arr.Add((const BYTE&) 0x00);