Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: ANN: AutoNe...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 11 of 13 Topic 9589 of 9984
Post > Topic >>

Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)

by "Alf P. Steinbach" <alfps@[EMAIL PROTECTED] > May 16, 2008 at 11:57 AM

* dizzy:
> * Alf P. Steinbach:
>> * Roland Pibinger:
>>>
>>> AFAICS, you need to sup****t const and non-const arguments.
>>
>> Well, if conventional, straightforward notation is to be sup****ted
>> then until C++0x that leads to a combinatorial explosion, e.g. 63 =
>> 2^(5+1)-1 constructor overloads for sup****t for 5 or fewer arguments,
>> and it's very rare that a non-const constructor argument makes sense.
>>
>> The only case I'm aware of where such sup****t could be beneficial is
>> for the ability to pass an rvalue of type T where T does not have a
>> T(T const&) copy constructor, e.g. rvalue of std::auto_ptr<U>.
>>
>> So this is a limitation of the trade-off chosen: it doesn't sup****t
>> actual constructor arguments that are rvalues of types like
>> std::auto_ptr (however, such types are exceeedingly rare).  Although I
>> didn't think of this particular problem, the general problem of
>> std::auto_ptr quirks was part of the reason why I experimented with
>> adapations of unique_ptr and move_ptr.  That code's still there.
>
> I thought the solution to this problem in C++03 was to have your
template
> arguments as non reference ones and if the user really needs to send you
a
> reference he/she will use some kind of reference wrapper objects (like
> boost::bind does and boost::ref/cref). Then you would sup****t auto_ptr
> too.

Thanks for that idea.  I wrote these forwarders almost on auto-pilot:
not much thinking and no research, just gut-feeling.  However, it
seems that inadvertently, purely by chance, I did the Right Thing(TM),
because some with-hindsight experimentation tells me now that with
formal argument type T it is difficult to avoid gross inefficiencies:


<code>
#include <iostream>
#include <ostream>
#include <string>

template< typename T >
struct IsRef { enum{ yes = 0 }; };

template< typename T >
struct IsRef<T&> { enum{ yes = 1 }; };

template< typename T >
char const* refSymbol() { return (IsRef<T>::yes? "&" : " "); }

template< typename T >
void foo( T x )
{
     std::cout
         << typeid(T).name() << refSymbol<T>() << ": " << x
         << std::endl;
}

template< typename T >
T const& constRef( T const& r ) { return r; }

int main()
{
     std::string s = "hula balula";

     foo( s );
     foo( constRef( s ) );
     foo<std::string const&>( s );
}
</code>


<output compiler="g++">
Ss : hula balula
Ss : hula balula
Ss&: hula balula
</output>


I.e., with a not-too-smart std::string, or user defined class, the
first two foo calls do copying with associated internal dynamic
allocation, and only the last, which is difficult (impossible?) for a
constructor, fixes this.

Of course, a demonstration that I'm able to do things in suboptimal
ways doesn't mean that there's no optimal way, but it indicates that
at least it would be hard for me to do without some further help...


Cheers,

- Alf

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

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




 13 Posts in Topic:
ANN: AutoNewPtr (oh yes, yet another smartpointer)
"Alf P. Steinbach&qu  2008-05-11 09:42:27 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
=?ISO-8859-1?Q?Daniel_Kr=  2008-05-11 16:39:48 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
"Alf P. Steinbach&qu  2008-05-11 20:13:20 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
"Alf P. Steinbach&qu  2008-05-11 20:13:18 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
"Alf P. Steinbach&qu  2008-05-12 04:38:03 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
rpbg123@[EMAIL PROTECTED]  2008-05-12 09:27:13 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
Kai-Uwe Bux <jkherciue  2008-05-13 21:13:58 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
"Alf P. Steinbach&qu  2008-05-14 12:53:54 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
"Alf P. Steinbach&qu  2008-05-14 15:11:54 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
dizzy <dizzy@[EMAIL PR  2008-05-15 11:16:50 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
"Alf P. Steinbach&qu  2008-05-16 11:57:29 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
rpbg123@[EMAIL PROTECTED]  2008-05-16 17:29:51 
Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
"Alf P. Steinbach&qu  2008-05-18 02:43:50 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Sep 7 4:23:34 CDT 2008.