shared_ptr and local types

T

Tim H

I'm newish to C++ but not to C.

I'm confused by this code. test1() is fine. test2() fails to
compile.

/tmp/inherit_ptr.cpp: In function âvoid test2()â:
/tmp/inherit_ptr.cpp:52: error: no matching function for call to
âboost::shared_ptr<Base>::shared_ptr(test2()::Derived2*)â
/usr/include/boost/shared_ptr.hpp:119: note: candidates are:
boost::shared_ptr<T>::shared_ptr() [with T = Base]
/usr/include/boost/shared_ptr.hpp:106: note:
boost::shared_ptr<Base>::shared_ptr(const boost::shared_ptr<Base>&)

Is there a general rule that I am violating here?

Tim



#include <iostream>
#include <string>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;

class Base {
public:
virtual ~Base() {}
virtual string evaluate() const = 0;
};
typedef shared_ptr<Base> Base_ptr;

//
// Test 1
//

// define Derived1 as a global type
class Derived1: public Base {
public:
Derived1(): Base() {}
virtual ~Derived1() {}
virtual string evaluate() const { return "Derived1"; }
};

void
test1()
{
// Base_ptr to subclass
Base_ptr base_der(new Derived1);
}

//
// Test 2
//

void
test2()
{
// define Derived2 as a local type
class Derived2: public Base {
public:
Derived2(): Base() {}
virtual ~Derived2() {}
virtual string evaluate() const { return "Derived2"; }
};

Base_ptr base_der(new Derived2);
}

int
main()
{
test1();
test2();
return 0;
}
 
P

Pete C

I'm newish to C++ but not to C.

I'm confused by this code. test1() is fine. test2() fails to
compile.

/tmp/inherit_ptr.cpp: In function âvoid test2()â:
/tmp/inherit_ptr.cpp:52: error: no matching function for call to
âboost::shared_ptr<Base>::shared_ptr(test2()::Derived2*)â
/usr/include/boost/shared_ptr.hpp:119: note: candidates are:
boost::shared_ptr<T>::shared_ptr() [with T = Base]
/usr/include/boost/shared_ptr.hpp:106: note:
boost::shared_ptr<Base>::shared_ptr(const boost::shared_ptr<Base>&)

Is there a general rule that I am violating here?


Yes - local types can't be used as template arguments (something do do
with having no linkage? I'm sure someone will correct / elaborate on
this).
I have never seen local class definitions in "real" code, and would
hesitate to use them because a) they're exotic and potentially
confusing, and b) the limitation you just ran into rather limits their
usefulness. Could you consider using an anonymous namespace instead?
 
T

Tim H

I'm newish to C++ but not to C.
I'm confused by this code. test1() is fine. test2() fails to
compile.
/tmp/inherit_ptr.cpp: In function âvoid test2()â:
/tmp/inherit_ptr.cpp:52: error: no matching function for call to
âboost::shared_ptr<Base>::shared_ptr(test2()::Derived2*)â
/usr/include/boost/shared_ptr.hpp:119: note: candidates are:
boost::shared_ptr<T>::shared_ptr() [with T = Base]
/usr/include/boost/shared_ptr.hpp:106: note:
boost::shared_ptr<Base>::shared_ptr(const boost::shared_ptr<Base>&)
Is there a general rule that I am violating here?

Yes - local types can't be used as template arguments (something do do
with having no linkage? I'm sure someone will correct / elaborate on
this).
I have never seen local class definitions in "real" code, and would
hesitate to use them because a) they're exotic and potentially
confusing, and b) the limitation you just ran into rather limits their
usefulness. Could you consider using an anonymous namespace instead?

The example arose as part of a unit-test, where the Derived2 class was
defined as part of the test function.

Tim
 
M

Marcus Kwok

Pete C said:
I'm newish to C++ but not to C.

I'm confused by this code. test1() is fine. test2() fails to
compile.

/tmp/inherit_ptr.cpp: In function âvoid test2()â:
/tmp/inherit_ptr.cpp:52: error: no matching function for call to
âboost::shared_ptr<Base>::shared_ptr(test2()::Derived2*)â
/usr/include/boost/shared_ptr.hpp:119: note: candidates are:
boost::shared_ptr<T>::shared_ptr() [with T = Base]
/usr/include/boost/shared_ptr.hpp:106: note:
boost::shared_ptr<Base>::shared_ptr(const boost::shared_ptr<Base>&)

Is there a general rule that I am violating here?


Yes - local types can't be used as template arguments (something do do
with having no linkage? I'm sure someone will correct / elaborate on
this).

Yes, I think the term is "external linkage", which local types do not
have.
I have never seen local class definitions in "real" code, and would
hesitate to use them because a) they're exotic and potentially
confusing, and b) the limitation you just ran into rather limits their
usefulness.

There have been several times when I wanted to declare a local/nested
class that served as a utility for the outer class but had no use
outside of this class; however, I could not use any of the <algorithm>s
or std containers on them because of the above reason. If I could use
templates with local classes, they would make my code slightly cleaner.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top