When i try to compile a program i got a bunch of errors like :'std::max': no matching overloaded function found

Viewed 612

When i try to compile a program i got a bunch of errors like :

'std::max': no matching overloaded function found

 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr) noexcept(<expr>)': expects 3 arguments - 2 provided


 C2782: 'const _Ty &std::max(const _Ty &,const _Ty &) noexcept(<expr>)': template parameter '_Ty' is ambiguous


  C2784: 'const _Ty &std::max(const _Ty &,const _Ty &) noexcept(<expr>)': could not deduce template argument for 'const _Ty &' from 'int'

Here is the code that cause most problems :

 template <typename TVar>
    void CopyVar( void*& pTarget, const void*& pSource, int nAlign = 4 )
    {
        *((TVar*) pTarget) = *((TVar*) pSource);
        ((BYTE*&) pTarget) += max( sizeof(TVar), nAlign );
        ((BYTE*&) pSource) += max( sizeof(TVar), nAlign );
    }

Can someone help me with this.

1 Answers

The sizeof operator returns a value of type size_t, which is an unsigned integer type.

Your call to std::max mixes two different types (the unsigned size_t and the signed int), and std::max requires both arguments to be of the same type.

As mentioned in a comment, a suitable solution is to make the nAlign variable the same type, i.e. a size_t. Alignment can never be negative, and is also a kind of a size. It also ensures that the types are the same size (size_t can be a 64-bit type, while unsigned int usually is 32 bits).

If changing the type of nAlign is not possible, then you should cast the result of sizeof to int (it's safer, since someone could pass a negative value for nAlign which will have bad consequences if casted to an unsigned type).

Related