F
Fokko Beekhof
Hello all,
please consider the following code:
--------------------------------------------------
#include <tr1/memory>
struct BaseA
{
int x;
};
struct BaseB
{
double x;
};
struct DerivA : public BaseA
{
int y;
};
struct DerivB : public BaseB
{
double y;
};
struct S
{
S(std::tr1::shared_ptr<BaseA> pa_) : pa(pa_) {}
S(std::tr1::shared_ptr<BaseB> pb_) : pb(pb_) {}
std::tr1::shared_ptr<BaseA> pa;
std::tr1::shared_ptr<BaseB> pb;
};
int main()
{
// S s(std::tr1::shared_ptr<BaseA>(new DerivA()) ); // works
// S s( new DerivA() ); // Doesn't work, SP constructor is explicit
S s(std::tr1::shared_ptr<DerivA>(new DerivA()) ); // breaks
return 0;
}
--------------------------------------------------
As you see, there are 3 possibilities in main(). #1 uses
std::tr1::shared_ptr<BaseA>, which works, like I would expect it to.
Then, #2 tries to pass a pointer where a shared_ptr is required, so that
doesn't work - I can understand that too, pointers cannot be implicitly
converted to shared_ptr.
My question:
Option #3, using a std::tr1::shared_ptr<DerivA>, does NOT work:
$ g++ -Wall ambiguousSP.cpp
ambiguousSP.cpp: In function ‘int main()’:
ambiguousSP.cpp:37: error: call of overloaded
‘S(std::tr1::shared_ptr<DerivA>)’ is ambiguous
ambiguousSP.cpp:28: note: candidates are: S::S(std::tr1::shared_ptr<BaseB>)
ambiguousSP.cpp:27: note: S:S(std::tr1::shared_ptr<BaseA>)
If I use "regular" pointers, it would works fine:
---------------------------------------------
struct BaseA
{
int x;
};
struct BaseB
{
double x;
};
struct DerivA : public BaseA
{
int y;
};
struct DerivB : public BaseB
{
double y;
};
struct S
{
S(BaseA * pa_) : pa(pa_), pb(0) {}
S(BaseB * pb_) : pa(0), pb(pb_) {}
BaseA * pa;
BaseB * pb;
};
int main()
{
S s(new DerivA()); // works fine
delete s.pa;
return 0;
}
---------------------------------------------
Could anyone comment on this ? Should option #3 with shared_ptr<DerivA>
work or not ?
Conceptually, I believe so. I can understand that the platform could
have some trouble with it, if it doesn't recognize that the template
parameter DerivA in shared_ptr<DerivA> is derived of BaseA.
Thanks for any comments,
F. Beekhof
please consider the following code:
--------------------------------------------------
#include <tr1/memory>
struct BaseA
{
int x;
};
struct BaseB
{
double x;
};
struct DerivA : public BaseA
{
int y;
};
struct DerivB : public BaseB
{
double y;
};
struct S
{
S(std::tr1::shared_ptr<BaseA> pa_) : pa(pa_) {}
S(std::tr1::shared_ptr<BaseB> pb_) : pb(pb_) {}
std::tr1::shared_ptr<BaseA> pa;
std::tr1::shared_ptr<BaseB> pb;
};
int main()
{
// S s(std::tr1::shared_ptr<BaseA>(new DerivA()) ); // works
// S s( new DerivA() ); // Doesn't work, SP constructor is explicit
S s(std::tr1::shared_ptr<DerivA>(new DerivA()) ); // breaks
return 0;
}
--------------------------------------------------
As you see, there are 3 possibilities in main(). #1 uses
std::tr1::shared_ptr<BaseA>, which works, like I would expect it to.
Then, #2 tries to pass a pointer where a shared_ptr is required, so that
doesn't work - I can understand that too, pointers cannot be implicitly
converted to shared_ptr.
My question:
Option #3, using a std::tr1::shared_ptr<DerivA>, does NOT work:
$ g++ -Wall ambiguousSP.cpp
ambiguousSP.cpp: In function ‘int main()’:
ambiguousSP.cpp:37: error: call of overloaded
‘S(std::tr1::shared_ptr<DerivA>)’ is ambiguous
ambiguousSP.cpp:28: note: candidates are: S::S(std::tr1::shared_ptr<BaseB>)
ambiguousSP.cpp:27: note: S:S(std::tr1::shared_ptr<BaseA>)
If I use "regular" pointers, it would works fine:
---------------------------------------------
struct BaseA
{
int x;
};
struct BaseB
{
double x;
};
struct DerivA : public BaseA
{
int y;
};
struct DerivB : public BaseB
{
double y;
};
struct S
{
S(BaseA * pa_) : pa(pa_), pb(0) {}
S(BaseB * pb_) : pa(0), pb(pb_) {}
BaseA * pa;
BaseB * pb;
};
int main()
{
S s(new DerivA()); // works fine
delete s.pa;
return 0;
}
---------------------------------------------
Could anyone comment on this ? Should option #3 with shared_ptr<DerivA>
work or not ?
Conceptually, I believe so. I can understand that the platform could
have some trouble with it, if it doesn't recognize that the template
parameter DerivA in shared_ptr<DerivA> is derived of BaseA.
Thanks for any comments,
F. Beekhof