473,603 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

lack of implicit casting to T* and boost::shared_p tr::get()

Does anyone know the reasons for the lack of an implicit casting
operator in any greater depth than:

A. Automatic conversion is believed to be too error prone.

(from the FAQ at the bottom of: http://www.boost.org/libs/smart_ptr/shared_ptr.htm)

Can anyone say specifically what about the implicit conversion would
be dangerous? I can think of a few things that would require care
about usage of smart pointers with implicit conversions, but nothing
that I would consider 'error-prone'.

In any language, there are implications and C++ certainly has its fair
share of other implicit behaviors.
Dec 20 '07 #1
5 2715
johanatan wrote:
Does anyone know the reasons for the lack of an implicit casting
operator in any greater depth than:

A. Automatic conversion is believed to be too error prone.

(from the FAQ at the bottom of: http://www.boost.org/libs/smart_ptr/shared_ptr.htm)

Can anyone say specifically what about the implicit conversion would
be dangerous? I can think of a few things that would require care
about usage of smart pointers with implicit conversions, but nothing
that I would consider 'error-prone'.
Ownership and reference counting issues? The last thing you want is for
the smart pointer to delete the object while something else still holds
a pointer to it.

One of the main reasons for using a smart pointer type is to manage the
lifetime of a object.

I've always avoided implicit conversions from smart pointers. Requiring
one is a design smell

--
Ian Collins.
Dec 20 '07 #2
Ownership and reference counting issues? The last thing you want is for
the smart pointer to delete the object while something else still holds
a pointer to it.
That was one of the few things that I had thought of. But, requiring
a get() method does little to help with that. Aren't you always just
going to call get() and use dumb ptr as you would have if it were
implicitly converted? To think that this will somehow alleviate the
programmer from reasoning about such things is quite naive (and in
fact, it could be argued that it gives novice programmers a false
sense of security). You still have to be careful with what you do
with a ptr that you obtain through get() as well as one implicitly
converted.
One of the main reasons for using a smart pointer type is to manage the
lifetime of a object.

I've always avoided implicit conversions from smart pointers. Requiring
one is a design smell
I suppose our senses of smell are subjective then. :-) I don't think
that C++, the language, was ever designed to prevent programmers from
doing stupid things (and boost is definitely breaking with the
tradition of C++ on this one). If 'safety' is what you want, you
might be using the wrong language. Any type of casting operator
overload is going to be 'unsafe' by this definition (not just those
relating to smart ptrs).

I've thought quite a bit about this and the only somewhat reasonable
situation I could see it being an issue is when converting a large
project over from dumb ptrs to smart ptrs (and missing one of the
conversions). But, if you are writing code from scratch, why wouldn't
you just use the smart ptrs everywhere? Performance impact is
minimal. If you get a situation where you really need to optimize for
performance, then isolate the bottleneck and write in assembly or C.
If you are careful enough to think about using dumb ptrs here for perf
reasons or smart ptrs there for convenience, then you should be
careful enough to be 'safe' doing it.
Dec 20 '07 #3
In message
<c3************ *************** *******@e23g200 0prf.googlegrou ps.com>,
johanatan <jo*******@gmai l.comwrites
>
>Ownership and reference counting issues? The last thing you want is for
the smart pointer to delete the object while something else still holds
a pointer to it.

That was one of the few things that I had thought of. But, requiring
a get() method does little to help with that. Aren't you always just
going to call get() and use dumb ptr as you would have if it were
implicitly converted?
No. Mostly you're going to call operator->() and not use get() at all.
To think that this will somehow alleviate the
programmer from reasoning about such things is quite naive (and in
fact, it could be argued that it gives novice programmers a false
sense of security). You still have to be careful with what you do
with a ptr that you obtain through get() as well as one implicitly
converted.
True, but on the whole once the raw pointer has been wrapped in the
smart one, you don't "obtain" the actual pointer at all, you use the ->
or * operators to access the pointed-to object.
>
>One of the main reasons for using a smart pointer type is to manage the
lifetime of a object.

I've always avoided implicit conversions from smart pointers. Requiring
one is a design smell

I suppose our senses of smell are subjective then. :-)
The problem with implicit conversion is that the smell only appears in
the declaration of the pointer class, not where the conversion gets
invoked. An explicit call of get() is highly visible in the code whereas
an implicit conversion is not.
I don't think
that C++, the language, was ever designed to prevent programmers from
doing stupid things (and boost is definitely breaking with the
tradition of C++ on this one). If 'safety' is what you want, you
might be using the wrong language. Any type of casting operator
overload is going to be 'unsafe' by this definition (not just those
relating to smart ptrs).
No doubt that's why many coding standards forbid them.
>I've thought quite a bit about this and the only somewhat reasonable
situation I could see it being an issue is when converting a large
project over from dumb ptrs to smart ptrs (and missing one of the
conversions) . But, if you are writing code from scratch, why wouldn't
you just use the smart ptrs everywhere?
Interfacing to legacy C-style libraries may require raw pointers. Other
than that, you're right - in which case, maybe you should be using a
smart pointer that has neither implicit conversion nor get().
>Performance impact is
minimal.
With any reasonable compiler, the performance impact of indirecting
through a smart pointer should be zero. It's only creating, copying and
deleting the pointers that has any overhead.
If you get a situation where you really need to optimize for
performance, then isolate the bottleneck and write in assembly or C.
If you are careful enough to think about using dumb ptrs here for perf
reasons or smart ptrs there for convenience, then you should be
careful enough to be 'safe' doing it.
And you're probably prematurely optimising anyway.

--
Richard Herring
Jan 10 '08 #4
On Jan 10, 3:47 pm, Richard Herring <ju**@[127.0.0.1]wrote:

[...]
With any reasonable compiler, the performance impact of
indirecting through a smart pointer should be zero. It's only
creating, copying and deleting the pointers that has any
overhead.
It depends. If the compiler doesn't see into the constructor of
the smart pointer, then it must assume that others can see it,
and modify it. That could affect optimization. (But it's hard
to imagine a scenario where the difference would be
significant.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 10 '08 #5
On Dec 20 2007, 4:28*am, johanatan <johana...@gmai l.comwrote:
>
I suppose our senses of smell are subjective then. :-) *I don't think
that C++, the language, was ever designed to prevent programmers from
doing stupid things (and boost is definitely breaking with the
tradition of C++ on this one). *If 'safety' is what you want, you
might be using the wrong language. *Any type of casting operator
overload is going to be 'unsafe' by this definition (not just those
relating to smart ptrs).
There's also history to consider. Back in the day, implicit
conversion operators were all the rage; years of experience
have made people aware of the pitfalls, few of which were
anticipated at the time. Hence e.g. std::string's has c_str()
whereas most pre-standard string libraries would provide an
operator const char*.
As a more general trend, of course, savvy C++ people are
moving away from the "unsafe" features of the language by
wrapping them in abstractions - even "naked" new/delete seems
to be frowned upon these days. The unsafe features are alive
and well, but are only used inside said abstractions which
are written by the more capable programmers, leaving the
ordinary rank and file with much less scope for doing the
wrong thing. So it's not that C++ might be the wrong language,
rather that one should be aware of how best practice C++
usage is evolving, with traditional patterns (that reflect
inexperience with the language and an over-reliance on C
idioms) being displaced by the safer/more robust ones that
have emerged over recent years.
Jan 10 '08 #6

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

Similar topics

4
3139
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented after A has been defined };
2
7230
by: Dennis Jones | last post by:
Hello, Given something like: boost::shared_ptr<T> t( new T() ); What is the best (correct?) way to dereference the pointer? The following two methods work. Is there a difference?
2
4822
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include "B.h"
2
2128
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using namespace System; #pragma unmanaged
6
4002
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
7
2963
by: myfavdepo | last post by:
Hi all, I have a query regarding the exchanging of a boost::shared_ptr beween different threads. In my program i've two threads both of which having their own internal queues for storing the shared_ptr. one thread is meant to pass the shared_ptr to another after its processing. So when a message arrives i convert it into a class of my own called 'Msg' and i put the 'Msg' object pointer into a shared_ptr and put into the other thread's...
9
9869
by: Christopher | last post by:
If a method is declared to return a type boost::shared_ptr<sometype>, how can the method be changed to do the equivalent of returning NULL when it was declared to return a raw pointer?
4
3942
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
1571
by: phlip | last post by:
Nick Keighley wrote: CC'd to the correct newsgroup. Yes, the destructor of the shared pointer will delete the object. Then its former address will convert to a reference. At some point - possibly the conversion - Undefined Behavior will begin. And then when whoever collects the reference uses it, Undefined Behavior will continue.
0
7996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7928
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8060
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6735
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3903
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.