Re: ANN: AutoNewPtr (oh yes, yet another smartpointer)
* dizzy:
* Alf P. Steinbach:
* Roland Pibinger:
AFAICS, you need to support const and non-const arguments.
Well, if conventional, straightforward notation is to be supported
then until C++0x that leads to a combinatorial explosion, e.g. 63 =
2^(5+1)-1 constructor overloads for support 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 support 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 support
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 support 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! ]