473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

CComPtr pass by reference issue with VC 2005 compiler

Due to the tightening of the VC++ compiler in 2005, I have run into a
compiler error (from code that previously worked in 2003) using a
CComPtr<ITypeLib> as an element of a std::list, as follows
std::list<CComPtr<ITypeLib> >. I understand the problem, so am looking
for the correct solution to my problem.

On the line that attempts to call push_back, I receive "error C2664:
'std::allocator<_Ty>::construct' : cannot convert parameter 1 from
'ITypeLib **' to 'ATL::CComPtr<T> *' c:\program files\microsoft visual
studio 8\vc\include\list 1163"

e.g.

std::list<CComPtr<ITypeLib> > typeLibs;

CComPtr<ITypeLib> spTypeLib;

... some code the creates an instance of spTypeLib

list.push_back(spTypeLib); // fails here with the C2664 error.

The problem is the push_back function takes a by reference parameter of
type CComPtr<ITypeLib>, however the CComPtr class overrides operator&,
returning a value of type ITypeLib**.

I want to use the CComPtr, for the benefits of the RAII pattern, and it
simplifies my production code significantly in this set of routines.

My question is "What is the correct way to handle this situation, and
avoid the overloaded operator& issue?"

Cheers,

Stuart

Dec 7 '05 #1
9 4631
Stuart Carnie wrote:
Due to the tightening of the VC++ compiler in 2005, I have run into a
compiler error (from code that previously worked in 2003) using a
CComPtr<ITypeLib> as an element of a std::list, as follows
std::list<CComPtr<ITypeLib> >. I understand the problem, so am looking
for the correct solution to my problem.

On the line that attempts to call push_back, I receive "error C2664:
'std::allocator<_Ty>::construct' : cannot convert parameter 1 from
'ITypeLib **' to 'ATL::CComPtr<T> *' c:\program files\microsoft
visual studio 8\vc\include\list 1163"

e.g.

std::list<CComPtr<ITypeLib> > typeLibs;

CComPtr<ITypeLib> spTypeLib;

.. some code the creates an instance of spTypeLib

list.push_back(spTypeLib); // fails here with the C2664 error.

The problem is the push_back function takes a by reference parameter of
type CComPtr<ITypeLib>, however the CComPtr class overrides operator&,
returning a value of type ITypeLib**.

I want to use the CComPtr, for the benefits of the RAII pattern, and it
simplifies my production code significantly in this set of routines.

My question is "What is the correct way to handle this situation, and
avoid the overloaded operator& issue?"

Cheers,

Stuart

Just want to add, that

typeLibs.push_back(reinterpret_cast<CComPtr<ITypeL ib> >(spTypeLib));

does not work, receive the following (suspicious) error

"error C2440: 'reinterpret_cast' : cannot convert from
'ATL::CComPtr<T>' to 'ATL::CComPtr<T>'
d:\sourcecode\slx_scorpion\projects\oledb_provider \commonsys\cdynamicobject.cpp
535"

Notice it says cannot convert from same type.
Dec 7 '05 #2
"Stuart Carnie" <st***********@nospam.nospam> wrote in message
news:%2*****************@TK2MSFTNGP10.phx.gbl...
Due to the tightening of the VC++ compiler in 2005, I have run into a
compiler error (from code that previously worked in 2003) using a
CComPtr<ITypeLib> as an element of a std::list, as follows
std::list<CComPtr<ITypeLib> >. I understand the problem, so am looking
for the correct solution to my problem.

On the line that attempts to call push_back, I receive "error C2664:
'std::allocator<_Ty>::construct' : cannot convert parameter 1 from
'ITypeLib **' to 'ATL::CComPtr<T> *' c:\program files\microsoft visual
studio 8\vc\include\list 1163"

e.g.

std::list<CComPtr<ITypeLib> > typeLibs;

CComPtr<ITypeLib> spTypeLib;

.. some code the creates an instance of spTypeLib

list.push_back(spTypeLib); // fails here with the C2664 error.

The problem is the push_back function takes a by reference parameter of
type CComPtr<ITypeLib>, however the CComPtr class overrides operator&,
returning a value of type ITypeLib**.

I want to use the CComPtr, for the benefits of the RAII pattern, and it
simplifies my production code significantly in this set of routines.

My question is "What is the correct way to handle this situation, and
avoid the overloaded operator& issue?"


The best solution is to avoid classes that overload operator & - they're
inherently incompatible with the standard library containers (despite the
fact that some combinations may actually work).

The only general soution that I know of is to wrap the type with another
type that forwards the entire public interface except for operator &. With
a smartpoint type, that'll no doubt cause some other problems somewhere
since it'll probably add a conversion to various sequences, which will in
turn make some implicit conversions no longer work (since they'll now
require two user defined conversions instead of one).

ATL actually supplies a wrapper for just this purpose: CAdapt<T>

-cd
Dec 7 '05 #3
Carl Daniel [VC++ MVP] wrote:
"Stuart Carnie" <st***********@nospam.nospam> wrote in message
news:%2*****************@TK2MSFTNGP10.phx.gbl...
Due to the tightening of the VC++ compiler in 2005, I have run into a
compiler error (from code that previously worked in 2003) using a
CComPtr<ITypeLib> as an element of a std::list, as follows
std::list<CComPtr<ITypeLib> >. I understand the problem, so am looking
for the correct solution to my problem.

On the line that attempts to call push_back, I receive "error C2664:
'std::allocator<_Ty>::construct' : cannot convert parameter 1 from
'ITypeLib **' to 'ATL::CComPtr<T> *' c:\program files\microsoft visual
studio 8\vc\include\list 1163"

e.g.

std::list<CComPtr<ITypeLib> > typeLibs;

CComPtr<ITypeLib> spTypeLib;

.. some code the creates an instance of spTypeLib

list.push_back(spTypeLib); // fails here with the C2664 error.

The problem is the push_back function takes a by reference parameter of
type CComPtr<ITypeLib>, however the CComPtr class overrides operator&,
returning a value of type ITypeLib**.

I want to use the CComPtr, for the benefits of the RAII pattern, and it
simplifies my production code significantly in this set of routines.

My question is "What is the correct way to handle this situation, and
avoid the overloaded operator& issue?"


The best solution is to avoid classes that overload operator & - they're
inherently incompatible with the standard library containers (despite the
fact that some combinations may actually work).

The only general soution that I know of is to wrap the type with another
type that forwards the entire public interface except for operator &. With
a smartpoint type, that'll no doubt cause some other problems somewhere
since it'll probably add a conversion to various sequences, which will in
turn make some implicit conversions no longer work (since they'll now
require two user defined conversions instead of one).

ATL actually supplies a wrapper for just this purpose: CAdapt<T>

-cd


Thanks for that..

Out of interest, I had to see what had changed, and it turns out to be
fairly simple:

In <list>, this method has changed between 2003 and 2005

_Nodeptr _Buynode(_Nodeptr _Next,
_Nodeptr _Prev, const _Ty& _Val)
{ // allocate a node and set links and value

// surrounding code removed for clarity

// this line causes the compilation issue.
// 2003 used the new operator to construct the object
this->_Alval.construct(&_Myval(_Pnode), _Val);

// code removed for clarity

}

Dec 7 '05 #4
Carl Daniel [VC++ MVP] wrote:
"Stuart Carnie" <st***********@nospam.nospam> wrote in message
news:%2*****************@TK2MSFTNGP10.phx.gbl...
Due to the tightening of the VC++ compiler in 2005, I have run into a
compiler error (from code that previously worked in 2003) using a
CComPtr<ITypeLib> as an element of a std::list, as follows
std::list<CComPtr<ITypeLib> >. I understand the problem, so am looking
for the correct solution to my problem.

On the line that attempts to call push_back, I receive "error C2664:
'std::allocator<_Ty>::construct' : cannot convert parameter 1 from
'ITypeLib **' to 'ATL::CComPtr<T> *' c:\program files\microsoft visual
studio 8\vc\include\list 1163"

e.g.

std::list<CComPtr<ITypeLib> > typeLibs;

CComPtr<ITypeLib> spTypeLib;

.. some code the creates an instance of spTypeLib

list.push_back(spTypeLib); // fails here with the C2664 error.

The problem is the push_back function takes a by reference parameter of
type CComPtr<ITypeLib>, however the CComPtr class overrides operator&,
returning a value of type ITypeLib**.

I want to use the CComPtr, for the benefits of the RAII pattern, and it
simplifies my production code significantly in this set of routines.

My question is "What is the correct way to handle this situation, and
avoid the overloaded operator& issue?"


The best solution is to avoid classes that overload operator & - they're
inherently incompatible with the standard library containers (despite the
fact that some combinations may actually work).

The only general soution that I know of is to wrap the type with another
type that forwards the entire public interface except for operator &. With
a smartpoint type, that'll no doubt cause some other problems somewhere
since it'll probably add a conversion to various sequences, which will in
turn make some implicit conversions no longer work (since they'll now
require two user defined conversions instead of one).

ATL actually supplies a wrapper for just this purpose: CAdapt<T>

-cd


Thanks for that..

Out of interest, I had to see what had changed, and it turns out to be
fairly simple:

In <list>, this method has changed between 2003 and 2005

_Nodeptr _Buynode(_Nodeptr _Next,
_Nodeptr _Prev, const _Ty& _Val)
{ // allocate a node and set links and value

// surrounding code removed for clarity

// this line causes the compilation issue.
// 2003 used the new operator to construct the object
this->_Alval.construct(&_Myval(_Pnode), _Val);

// code removed for clarity

}

Dec 7 '05 #5
Carl Daniel [VC++ MVP] wrote:
"Stuart Carnie" <st***********@nospam.nospam> wrote in message
news:%2*****************@TK2MSFTNGP10.phx.gbl...
Due to the tightening of the VC++ compiler in 2005, I have run into a
compiler error (from code that previously worked in 2003) using a
CComPtr<ITypeLib> as an element of a std::list, as follows
std::list<CComPtr<ITypeLib> >. I understand the problem, so am looking
for the correct solution to my problem.

On the line that attempts to call push_back, I receive "error C2664:
'std::allocator<_Ty>::construct' : cannot convert parameter 1 from
'ITypeLib **' to 'ATL::CComPtr<T> *' c:\program files\microsoft visual
studio 8\vc\include\list 1163"

e.g.

std::list<CComPtr<ITypeLib> > typeLibs;

CComPtr<ITypeLib> spTypeLib;

.. some code the creates an instance of spTypeLib

list.push_back(spTypeLib); // fails here with the C2664 error.

The problem is the push_back function takes a by reference parameter of
type CComPtr<ITypeLib>, however the CComPtr class overrides operator&,
returning a value of type ITypeLib**.

I want to use the CComPtr, for the benefits of the RAII pattern, and it
simplifies my production code significantly in this set of routines.

My question is "What is the correct way to handle this situation, and
avoid the overloaded operator& issue?"


The best solution is to avoid classes that overload operator & - they're
inherently incompatible with the standard library containers (despite the
fact that some combinations may actually work).

The only general soution that I know of is to wrap the type with another
type that forwards the entire public interface except for operator &. With
a smartpoint type, that'll no doubt cause some other problems somewhere
since it'll probably add a conversion to various sequences, which will in
turn make some implicit conversions no longer work (since they'll now
require two user defined conversions instead of one).

ATL actually supplies a wrapper for just this purpose: CAdapt<T>

-cd


Thanks for that..

Out of interest, I had to see what had changed, and it turns out to be
fairly simple:

In <list>, this method has changed between 2003 and 2005

_Nodeptr _Buynode(_Nodeptr _Next,
_Nodeptr _Prev, const _Ty& _Val)
{ // allocate a node and set links and value

// surrounding code removed for clarity

// this line causes the compilation issue.
// 2003 used the new operator to construct the object
this->_Alval.construct(&_Myval(_Pnode), _Val);

// code removed for clarity

}

Dec 7 '05 #6
Carl Daniel [VC++ MVP] wrote:
"Stuart Carnie" <st***********@nospam.nospam> wrote in message
news:%2*****************@TK2MSFTNGP10.phx.gbl...
Due to the tightening of the VC++ compiler in 2005, I have run into a
compiler error (from code that previously worked in 2003) using a
CComPtr<ITypeLib> as an element of a std::list, as follows
std::list<CComPtr<ITypeLib> >. I understand the problem, so am looking
for the correct solution to my problem.

On the line that attempts to call push_back, I receive "error C2664:
'std::allocator<_Ty>::construct' : cannot convert parameter 1 from
'ITypeLib **' to 'ATL::CComPtr<T> *' c:\program files\microsoft visual
studio 8\vc\include\list 1163"

e.g.

std::list<CComPtr<ITypeLib> > typeLibs;

CComPtr<ITypeLib> spTypeLib;

.. some code the creates an instance of spTypeLib

list.push_back(spTypeLib); // fails here with the C2664 error.

The problem is the push_back function takes a by reference parameter of
type CComPtr<ITypeLib>, however the CComPtr class overrides operator&,
returning a value of type ITypeLib**.

I want to use the CComPtr, for the benefits of the RAII pattern, and it
simplifies my production code significantly in this set of routines.

My question is "What is the correct way to handle this situation, and
avoid the overloaded operator& issue?"


The best solution is to avoid classes that overload operator & - they're
inherently incompatible with the standard library containers (despite the
fact that some combinations may actually work).

The only general soution that I know of is to wrap the type with another
type that forwards the entire public interface except for operator &. With
a smartpoint type, that'll no doubt cause some other problems somewhere
since it'll probably add a conversion to various sequences, which will in
turn make some implicit conversions no longer work (since they'll now
require two user defined conversions instead of one).

ATL actually supplies a wrapper for just this purpose: CAdapt<T>

-cd


Thanks for that..

Out of interest, I had to see what had changed, and it turns out to be
fairly simple:

In <list>, this method has changed between 2003 and 2005

_Nodeptr _Buynode(_Nodeptr _Next,
_Nodeptr _Prev, const _Ty& _Val)
{ // allocate a node and set links and value

// surrounding code removed for clarity

// this line causes the compilation issue.
// 2003 used the new operator to construct the object
this->_Alval.construct(&_Myval(_Pnode), _Val);

// code removed for clarity

}
Dec 9 '05 #7
> My question is "What is the correct way to handle this situation, and
avoid the overloaded operator& issue?"


Use:

std::list<CComPtr<CAdapt <ITypeLib> > > typeLibs;

Brian
Dec 9 '05 #8
"Brian Muth" <bm***@mvps.org> wrote in message
news:uj****************@TK2MSFTNGP11.phx.gbl...
My question is "What is the correct way to handle this situation, and
avoid the overloaded operator& issue?"


Use:

std::list<CComPtr<CAdapt <ITypeLib> > > typeLibs;


Make that

std::list<CAdapt<CComPtr<ITypeLib> > > typeLibs;

-cd
Dec 10 '05 #9

Make that

std::list<CAdapt<CComPtr<ITypeLib> > > typeLibs;

-cd


(embarrassed look)

Thanks, Carl.

Brian
Dec 10 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
2
by: Benny | last post by:
I need to pass a reference of a managed object to a function (bar). I believe the syntax is correct. When I build the project, a message box with the title "Microsoft (R) C/C++ Optimizing...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
7
by: Jess | last post by:
Hello, I learned that when I work with templates in C++, I should have functions that pass arguments by reference because the type of object is not known. Does it mean that if I have a function...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.