473,769 Members | 5,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ambiguity with Smart Pointers (Boost or similar)

I've used Boost for this example; in fact we have our own pointer
class, for historic reasons.

#include "boost\shared_p tr.hpp"

// A heirarchy of classes
class G1 {};
class G2: public G1 {};

// A method which takes a shared pointer to the base class
void f(boost::shared _ptr<G1>) {};

// A method which takes a shared pointer to some other class
void f(boost::shared _ptr<int>) {};

void main()
{
// Call a method with a shared pointer to a derived class
f(boost::shared _ptr<G2>());
}

This fails, because the compiler is unable to decide which of the two
methods is meant. Of course, to a human, it's pretty obvious which
one is meant, but there doesn't seem to be enough information at the
time for the compiler to decide. In practice I have several hundred
functions, and 50 or so classes, to get confused among, so I don't
really want to take the obvious route [declare an
f(boost::shared _ptr<G2>) ]. Is there anything I can do to the pointer
class, or even to the class inheritance, that'll help the compiler
out?

Thx
Jan 3 '08 #1
5 1543
On 3 Jan, 12:11, number...@netsc ape.net wrote:
I've used Boost for this example; *in fact we have our own pointer
class, for historic reasons.

#include "boost\shared_p tr.hpp"

// A heirarchy of classes
class G1 {};
class G2: public G1 {};

// A method which takes a shared pointer to the base class
void f(boost::shared _ptr<G1>) {};

// A method which takes a shared pointer to some other class
void f(boost::shared _ptr<int>) {};

void main()
{
* * * * // Call a method with a shared pointer to a derived class
* * * * f(boost::shared _ptr<G2>());

}

This fails, because the compiler is unable to decide which of the two
methods is meant. *Of course, to a human, it's pretty obvious which
one is meant, but there doesn't seem to be enough information at the
time for the compiler to decide. *In practice I have several hundred
functions, and 50 or so classes, to get confused among, so I don't
really want to take the obvious route [declare an
f(boost::shared _ptr<G2>) ]. *Is there anything I can do to the pointer
class, or even to the class inheritance, that'll help the compiler
out?

Thx
I don't use boost::shared_p tr, but I am surprised if it cant
discriminate the above case, though maybe this is the result of an old
compiler, which may mean it cant use some facilities.

Hint! use some SFINAE, in combination with std::tr1 type_traits, e.g
is_base_of etc, but as I said a good smart pointer should easily
handle this case automatically.

regards
Andy Little
Jan 3 '08 #2
On Jan 3, 12:57 pm, kwikius <a...@servocomm .freeserve.co.u kwrote:
>
I don't use boost::shared_p tr, but I am surprised if it cant
discriminate the above case, though maybe this is the result of an old
compiler, which may mean it cant use some facilities.
Fails with MS VC 2005 AND Gcc 4.01 on an Apple...
Hint! use some SFINAE, in combination with std::tr1 type_traits, e.g
is_base_of etc, but as I said a good smart pointer should easily
handle this case automatically.
If you know a better smart pointer, please let me know :) .
Meanwhile, I'll keep playing. is_base_of isn't in my copy of the
standard [2003, 2nd ed] but there are web refs to it in Boost which
I'll read.

Thanks
Jan 3 '08 #3
On 3 Jan, 14:17, kwikius <a...@servocomm .freeserve.co.u kwrote:
* * * typename quanta::where_<
* * * * *std::tr1::is_b ase_of<T,Derive d>,
* * * * *void*
* * * >::type* =0
...Oops try changing quanta::where to enable_if ;-)

regards
Andy Little

Jan 3 '08 #4
On Jan 3, 7:11 am, number...@netsc ape.net wrote:
I've used Boost for this example; in fact we have our own pointer
class, for historic reasons.

#include "boost\shared_p tr.hpp"

// A heirarchy of classes
class G1 {};
class G2: public G1 {};

// A method which takes a shared pointer to the base class
void f(boost::shared _ptr<G1>) {};

// A method which takes a shared pointer to some other class
void f(boost::shared _ptr<int>) {};

void main()
{
// Call a method with a shared pointer to a derived class
f(boost::shared _ptr<G2>());

}

This fails, because the compiler is unable to decide which of the two
methods is meant. Of course, to a human, it's pretty obvious which
one is meant, but there doesn't seem to be enough information at the
time for the compiler to decide. In practice I have several hundred
functions, and 50 or so classes, to get confused among, so I don't
really want to take the obvious route [declare an
f(boost::shared _ptr<G2>) ]. Is there anything I can do to the pointer
class, or even to the class inheritance, that'll help the compiler
out?

Thx
How about templates:

#include <iostream>
#include "boost/shared_ptr.hpp"

class G1
{
public:
~G1() { std::cout << "~G1()\n"; }
};

class G2: public G1
{
public:
~G2() { std::cout << "~G2()\n"; }
};

template< typename T >
void f(boost::shared _ptr< T bsp)
{
std::cout << "void f(boost::shared _ptr< T >&)\n";
}

template<>
void f(boost::shared _ptr<G1bsp)
{
std::cout << "void f(boost::shared _ptr<G1>&)\n";
}

template<>
void f(boost::shared _ptr<intbsp)
{
std::cout << "void f(boost::shared _ptr<int>&)\n";
}

int main()
{
f(boost::shared _ptr<G1>(new G2));
}

/*
void f(boost::shared _ptr<G1>&)
~G2()
~G1() // even though that dtor isn't virtual!
*/
Jan 3 '08 #5
On Jan 3, 4:27 pm, Salt_Peter <pj_h...@yahoo. comwrote:
<snip>
int main()
{
f(boost::shared _ptr<G1>(new G2));
}
^^^^^^^^^^^^^^^ ^^^^^^
That's the critical bit. (or at least, if you are in the same font as
me :) ) If the pointer passed to f *is* a shared_ptr<G1th en there's
no confusion. It's only when it's some other type that the compiler
goes hunting for a match - and finds two.

--
Old Faithful
Jan 3 '08 #6

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

Similar topics

14
2685
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". Essentially, the smart guarantee promises to clean up resources whose ownership is passed into the function, for whatever defintion of "clean up" is most appropriate for the resource passed. Note that this is different from both the basic and the...
5
5064
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too stupid to understand and make them work. E.g. I have read that boost's smart pointers are able to do this convertion, but the following code doesn't compile (VC++6.0):
2
1796
by: Vijayaraghavan Kalyanapasupathy | last post by:
Hello All, I just learnt about smart pointers. Given the advantages of using smart-pointers, as a designer of say a List object, I can inhibit creation of the actual objects by making its constructors and then making the smart pointer a friend of the class (I am talking about a specific smart pointer implementation for a specific object). Now, it seems to me that inhibiting the use of "new" with the smart pointer would enhance things....
8
5153
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good documentation like the following: http://axter.com/smartptr Now I'm on a client site, and I'm trying to create the same type of documentation on a very large project. I ran the Doxygen program, and it ran for over 16 hours, before I had
6
2309
by: zl2k | last post by:
hi, When I considered about preventing memory leaking, the method came up to my mind is using boost smart pointer if possible (use stl::vector instead of type, use smart pointer whenever declare an instance of class). With the container of STL, this may often result in a vector of smart pointers, or a smart pointer of STL container. Am I making things too complicated or this is an usual implementation? Thanks in advance. zl2k
92
5118
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
33
5083
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
54
12019
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously concerned that there is a shift to raw pointers. We are not developing system software but rather...
2
2387
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
I have a problem with a cyclic dependency of two classes: class Iref_count // Interface for the intrusive_ptr { friend class int_ptr_base; // for access to Count private: volatile unsigned Count; protected: Iref_count() : Count(0) {} // You must not call the non-virtual destructor directly.
0
9589
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
9423
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,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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
8872
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.