class - C++ compilation error, ambigous operator overloads -


i have template array class overloading both operator [] access items , operator t * direct buffer access.

template< typename t > class buffer {     const t & operator [] ( size_t ) const;           t & operator [] ( size_t );      operator const t * () const;     operator       t * (); }; 

for reason, using operator [] on instance of class generates compilation error saying there 4 possible overloads.

buffer< int > buf; buf[ some_position ] = 0; // generates error 

error :

error   3   error c2666: 'xxx::buffer<t>::operator []' : 4 overloads have similar conversions   c:\xxx.cpp  3886 

is operator [] trying convert instance buf t * ? why 4 overloads detected instead of 2 ? thank you. :)

edit : : buf[ some_position ] = 0; // generates error

the issue have implicit conversions going in both directions.

disregarding const, 2 candidates are:

t & buffer::operator [] ( size_t ); int& operator[] (int*, int); 

for call buf[pos] pos int (such literal), call ambiguous. first candidate selected converting pos std::size_t , second selected converting buf int*.

you disambiguate call explicitly casting argument std::size_t, or modifying operator take int instead.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -