Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Avoid accidentally creating a temporary

Reply
Thread Tools

Avoid accidentally creating a temporary

 
 
Phil Endecott
Guest
Posts: n/a
 
      05-15-2011
On May 15, 7:39 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
(E-Mail Removed)> wrote:
> Why are you replying to your own post?


Because I'm adding more information that's not related to any of the
replies. Which post would you prefer me to reply to?

> Anyway, see my reply.


Your suggestion requires that I change all the places where I use the
class; that's not ideal.

> And also, you might want to look up the ScopeGuard article by Marginean and
> Alexandrescu in DDJ.


You mean http://drdobbs.com/cpp/184403758 ? I guess the relevant bit
is that they define a macro that hides some of this. Yes, I guess I
could do that:

#define SCOPED_FOO(P) scoped_foo F_#LINE(P)

....or similar, I forget the LINE details. Thanks for the suggestion.


Phil.
 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      05-15-2011
Johannes Schaub wrote:

> Alf P. Steinbach /Usenet wrote:
>
>> <code>
>> #include <stdio.h>
>> #include <assert.h>
>>
>> void enable_foo( int ) { printf( "e\n" ); }
>> void disable_foo() { printf( "d\n" ); }
>>
>> namespace detail
>> {
>> struct scoped_foo_impl
>> {
>> scoped_foo_impl( scoped_foo_impl const& )
>> {
>> assert( "Ouch, copy constructing! Probably no RVO..." );
>> }
>>
>> explicit scoped_foo_impl( int n )
>> {
>> enable_foo( n );
>> }
>>
>> ~scoped_foo_impl() { disable_foo(); }
>> };
>>
>> scoped_foo_impl make_foo( int n ) { return scoped_foo_impl( n );
>> }
>> }
>>
>> typedef detail::scoped_foo_impl const& scoped_foo;
>> using detail::make_foo;
>>
>> int main()
>> {
>> scoped_foo g = make_foo( 3 );
>> //scoped_foo make_foo( 3 ); // Nix!
>> //scoped_foo( 3 ); // Nyet!
>>
>> printf( "*\n" );
>> }
>> </code>

>
> This is a neat trick, I will have to remember it. Sadly, it doesn't work
> with list initialization
>
> scoped_foo make_foo{ 3 }; // valid
> scoped_foo { 3 }; // valid


If you're introducing a factory:

class A; A make_A(int n);

class A {
// implicit private:
A(int){}
friend A make_A(int n){return A(n);}
};
int main(){
A a=make_A(3);
}

(you can still couple that with the various tricks above)

Easiest remains the macro:
#define takefoo(X) scoped_foo foo##__LINE__ (X);
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      05-15-2011
* Phil Endecott, on 15.05.2011 21:28:
> On May 15, 7:39 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
> (E-Mail Removed)> wrote:
>> Why are you replying to your own post?

>
> Because I'm adding more information that's not related to any of the
> replies. Which post would you prefer me to reply to?


I'd prefer that you start new threads for new problems.


>> Anyway, see my reply.

>
> Your suggestion requires that I change all the places where I use the
> class; that's not ideal.


If you want to check whether existing usages are wrong (just temporaries), then
that's a different problem.

AFAIK it has no solution within the C++ language.

Instead you'd have to parse your C++ code, manually or by creating some tool to
do it. I would suggest just grepping the lines with the class name, then
checking those lines. It would not pick up typedefs or macros etc., but as a
practical matter it would probably suffice for what you now say the problem is.


>> And also, you might want to look up the ScopeGuard article by Marginean and
>> Alexandrescu in DDJ.

>
> You mean http://drdobbs.com/cpp/184403758 ? I guess the relevant bit
> is that they define a macro that hides some of this. Yes, I guess I
> could do that:
>
> #define SCOPED_FOO(P) scoped_foo F_#LINE(P)
>
> ...or similar, I forget the LINE details. Thanks for the suggestion.


You're welcome.

However, note that also scopeguard macros requires changing the places where
you're using the class, so wrt. your criteria it's "not ideal".

Also, note that MSVC with edit-and-continue option Z-something is very
non-standard wrt. the __LINE__ macro, so Marginean's code needs to be
special-cased for MSVC (using e.g. MSVC's language extension __COUNTER__).


Cheers & hth.,

- Alf "not surprised"

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accidentally Deleted a Folder in Outlook Express amanda.wolkenhauer@gmail.com Computer Support 6 12-15-2007 05:38 PM
How to protect the LCD from being scratched accidentally? dalimoh@gmail.com Digital Photography 41 03-30-2007 07:58 AM
! "My documents" folder accidentally overwritten Rock Gibbons Computer Support 21 03-26-2007 11:39 PM
I Want To Retrieve Files On A 20D CF Card That I Accidentally Formatted Clyde Torres Digital Photography 3 10-22-2004 02:25 AM
Are there any free utilities to recover an accidentally deleted photo from a SmartMedia card? sm9 Digital Photography 12 08-14-2003 11:27 AM



Advertisments