Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error C2784 while creating allocator

4 views
Skip to first unread message

mis...@my-deja.com

unread,
Jan 19, 2001, 4:02:02 AM1/19/01
to
I'm programming a user-defined allocator to use
with the STL-containers. I followed the tutorial
of Nicolai Josuttis' book on the STL. My
implementation seemed to work well with deques
and vectors. But then I tried to use the 'swap'-
member function of a vector and I get this
annoying list of C2784-errors, which is hard to
understand (for me, that is). The problem seems
to lie in the 'operator=='/'!=':

/* Return that all specializations of this
allocator are interchangeable */

template <class T1, class T2>
bool operator== (const std::mbr_allocator<T1>&,
const std::mbr_allocator<T2>&) {
return true;
}

template <class T1, class T2>
bool operator!= (const std::mbr_allocator<T1>&,
const std::mbr_allocator<T2>&) {
return false;
}


The errors all look like the first in the
sequence:

c:\program files\microsoft visual studio\vc98
\include\vector(220) : error C2784: 'bool __cdecl
std::operator ==(const class std::list<_Ty,_A>
&,const class std::list<_Ty,_A> &)' : could not
deduce template argument for 'const class
std::list<_Ty,_A>
&' from 'class std::mbr_allocator<int>'
c:\program files\microsoft visual
studio\vc98\include\vector(220) : while compiling
class-template member function 'void __thiscall
std::vector<int,class std::mbr_allocator<int>
>::swap(class std::vector<int,class
std::mbr_allocator<int> > &
)'

I hope anyone can explain the cause of these
errors and can come up with a solution.

Mischa


Sent via Deja.com
http://www.deja.com/

Craig Powers

unread,
Jan 19, 2001, 9:08:59 AM1/19/01
to
mis...@my-deja.com wrote:
>
> I'm programming a user-defined allocator to use
> with the STL-containers. I followed the tutorial
> of Nicolai Josuttis' book on the STL. My
> implementation seemed to work well with deques
> and vectors. But then I tried to use the 'swap'-
> member function of a vector and I get this
> annoying list of C2784-errors, which is hard to
> understand (for me, that is). The problem seems
> to lie in the 'operator=='/'!=':
>
> /* Return that all specializations of this
> allocator are interchangeable */
>
> template <class T1, class T2>
> bool operator== (const std::mbr_allocator<T1>&,
> const std::mbr_allocator<T2>&) {
> return true;
> }
>
> template <class T1, class T2>
> bool operator!= (const std::mbr_allocator<T1>&,
> const std::mbr_allocator<T2>&) {
> return false;
> }

Are you declaring your operators as const functions?

Tom

unread,
Jan 20, 2001, 7:31:43 AM1/20/01
to

It does seem to be a problem with comparing the allocators as you say
(having peeked at vector line 220). For some reason it isn't finding
your operator== function at all, and is trying a random other one
instead (in this case the std::list one).

The problem seems to be that you've put the allocator in namespace
std, which is completely illegal C++. At the same time I suspect that
your operator== function isn't in namespace std, and hence is not
being found by argument dependent lookup on operator==. The solution:
take your allocator out of namespace std an put it in the global
namespace, or in one of your own namespaces. Put the operator== in the
exact same namespace to ensure that it is found by ADL.

Tom

mis...@my-deja.com

unread,
Jan 22, 2001, 3:14:38 AM1/22/01
to
In article <3a698529...@news.ntlworld.com>,

the...@my-deja.com (Tom) wrote:
>
> It does seem to be a problem with comparing the allocators as you say
> (having peeked at vector line 220). For some reason it isn't finding
> your operator== function at all, and is trying a random other one
> instead (in this case the std::list one).
>
> The problem seems to be that you've put the allocator in namespace
> std, which is completely illegal C++. At the same time I suspect that
> your operator== function isn't in namespace std, and hence is not
> being found by argument dependent lookup on operator==. The solution:
> take your allocator out of namespace std an put it in the global
> namespace, or in one of your own namespaces. Put the operator== in the
> exact same namespace to ensure that it is found by ADL.
>
> Tom

Thanks very much! Your answer indeed solved the problem. Now onto the
next one...:-)

0 new messages