473,408 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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_ptr.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 1523
On 3 Jan, 12:11, number...@netscape.net wrote:
I've used Boost for this example; *in fact we have our own pointer
class, for historic reasons.

#include "boost\shared_ptr.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_ptr, 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.ukwrote:
>
I don't use boost::shared_ptr, 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.ukwrote:
* * * typename quanta::where_<
* * * * *std::tr1::is_base_of<T,Derived>,
* * * * *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...@netscape.net wrote:
I've used Boost for this example; in fact we have our own pointer
class, for historic reasons.

#include "boost\shared_ptr.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<G1then 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
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". ...
5
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...
2
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...
8
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...
6
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...
92
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,...
33
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...
54
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...
2
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.