C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

implicit conversion chains

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jason Hise
Guest





PostPosted: Sun Jul 10, 2005 9:38 am    Post subject: implicit conversion chains Reply with quote



I want to create a reference wrapper, which performs some locking when
implicitly converted to a raw reference. I thought that this could be
accomplished through the creation of an intermediate temporary object
containing the lock.

Unfortunately, the introduction of the intermediate type is causing the
compiler to no longer see a way to convert from the reference wrapper
to the raw reference. Why is this chain of implicit conversions not
recognized? Is there a correct way to implement such chains?

Following is some stripped down sample code that demonstrates the
problem:

// my locking reference type
class ref
{
private:
struct locked_ref
{
operator int & ( );
lock my_lock;
};

public:
operator locked_ref ( );
};

// from some other library that client code is using
void foo ( int & );

// I want client code to be able to do:
void bar ( ref i )
{
foo ( i ); // temporary locked_ref should be created here and last
for the duration of the call to foo
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gianluca Silvestri
Guest





PostPosted: Mon Jul 11, 2005 3:10 pm    Post subject: Re: implicit conversion chains Reply with quote




"Jason Hise" <0xCHAOS (AT) gmail (DOT) com> ha scritto nel messaggio
news:1120980892.112395.148500 (AT) g43g2000cwa (DOT) googlegroups.com...
Quote:
I want to create a reference wrapper, which performs some locking when
implicitly converted to a raw reference. I thought that this could be
accomplished through the creation of an intermediate temporary object
containing the lock.

Unfortunately, the introduction of the intermediate type is causing the
compiler to no longer see a way to convert from the reference wrapper
to the raw reference. Why is this chain of implicit conversions not
recognized? Is there a correct way to implement such chains?

Two user-defined conversions are involved here. The standard allows at most
1 user-defined conversion in an Implicit Conversion Sequence.
I dont' know if it's feasible for you because you should have provided more
code to look, but perhaps you can eliminate 1 udc by acquiring the lock in
operator int&() function.

HTH,
Gianluca



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jason Hise
Guest





PostPosted: Tue Jul 12, 2005 12:43 am    Post subject: Re: implicit conversion chains Reply with quote



The problem is that I want to create a temporary object at the point of
conversion, which lasts as long as the statement where the conversion
takes place. I could possibly perform locking at the point when the
conversion is called, but then I would have no way of automatically
releasing the lock. I am essentially trying to mirror a smart pointer
idiom, in which using the -> operator results in the creation of a
temporary locking pointer object which serializes the operation.

As I am mainly providing the smart reference type to be the result of
dereferencing a smart pointer type (via operator *), I suppose it may
be reasonable for the outer smart reference itself to own a lock rather
than waiting until the conversion to a raw reference takes place to
serialize access.

-Jason


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gianluca Silvestri
Guest





PostPosted: Tue Jul 12, 2005 8:34 am    Post subject: Re: implicit conversion chains Reply with quote


"Jason Hise" <0xCHAOS (AT) gmail (DOT) com> ha scritto nel messaggio
news:1121098384.769101.191020 (AT) g14g2000cwa (DOT) googlegroups.com...
Quote:
The problem is that I want to create a temporary object at the point of
conversion, which lasts as long as the statement where the conversion
takes place. I could possibly perform locking at the point when the
conversion is called, but then I would have no way of automatically
releasing the lock. I am essentially trying to mirror a smart pointer
idiom, in which using the -> operator results in the creation of a
temporary locking pointer object which serializes the operation.

As I am mainly providing the smart reference type to be the result of
dereferencing a smart pointer type (via operator *), I suppose it may
be reasonable for the outer smart reference itself to own a lock rather
than waiting until the conversion to a raw reference takes place to
serialize access.


I don't see many options. Either you modify your ref class to be a
locked_ref itself, or you use Lock class to acquire the lock prior to the
call or you can do something like this:

class ref
{
struct locked
{
ref& obj;
locked(ref& obj_,/*the lock object by ref*/)
: obj(obj_)
~locked()
{
//release lock
}
operator int&();
};
public:
locked lock() { /*acquire lock here and then */return locked(*this,/*the
lock object*/); }
};

// from some other library that client code is using
void foo ( int & );

// I want client code to be able to do:
void bar ( ref i )
{
foo ( i.lock() );
}

See also http://www.research.att.com/~bs/wrapper.pdf

HTH
Gianluca




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.