I have two shared_ptrs of the classes U and T where T is the base of U.
It is no problem to do an implicit conversion from shared_ptr<U> to shared_ptr<T>.
But is is also possible to do a conversion from shared_ptr<T> to shared_ptr<U> ?
I tried the sugested solution:
class T {
public:
virtual ~T() {}
protected:
void fillData() = 0;
};
class Timpl : public T
{
public:
virtual ~Timpl() {}
protected:
virtual void fillData() = 0;
};
class U : public Timpl {
public:
virtual ~U() {}
protected:
virtual void fillData() {...} // fill
};
typedef shared_ptr<T> TPtr
typedef shared_ptr<U> UPtr
TPtr tp = std::make_shared<U>();
UPtr up = std::static_pointer_cast<U>(tp); //<-- error here :
error: no matching function for call to 'static_pointer_cast(TPtr)'
note: template std::__shared_ptr<_Tp1, _Lp> std::static_pointer_cast(const std::__shared_ptr<_Tp2, _Lp>&)
note: template argument deduction/substitution failed:
note: 'TPtr {aka boost::shared_ptr}' is not derived from 'const std::__shared_ptr<_Tp2, _Lp>'
Solution for this issue:
mixing of std::shared_ptr with boost::shared_ptr is no good idea.
Yes, use static_pointer_cast:
#include <memory>
struct T { virtual ~T() {} };
struct U : T {};
std::shared_ptr<T> pt = std::make_shared<U>(); // *pt is really a "U"
auto pu = std::static_pointer_cast<U>(pt);
There's also a matching std::dynamic_pointer_cast which returns a null pointer if the conversion isn't possible.
Related
I can't understand how i can create expr object from double in expr.cpp file.
expr_base:
The base class of all expressions. Note that all expression classes
(including this base) are private to the implementation and should not
be exposed to other code. The rest of the program should use
expressions only via expr.
This subclasses std::enable_shared_from_this to enable getting
shared_ptr to this from a method.
expr:
Wrapper around dynamically allocated instances of expr_base. This type
has value semantics and since all subclasses of expr_base are
immutable, shallow copies are made.
This type has overloaded functions and operators, so that expression
construction is easy and readable.
error:
error: no viable conversion from returned value of type 'typename enable_if<!is_array<number>::value,
shared_ptr<number> >::type' (aka 'std::__1::shared_ptr<exprs::number>') to function return type 'expr'
return std::make_shared<exprs::number>(n);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/..../expr.hpp(...): note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'typename enable_if<!is_array<number>::value, shared_ptr<number> >::type' (aka 'std::__1::shared_ptr<exprs::number>') to 'const expr &' for 1st argument
class expr final {
^
/.../expr.hpp:(...): note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'typename enable_if<!is_array<number>::value, shared_ptr<number> >::type' (aka 'std::__1::shared_ptr<exprs::number>') to 'expr &&' for 1st argument
/.../expr.hpp:(...): note: candidate template ignored: requirement 'std::is_convertible<exprs::number *, const expr_base*>::value' was not satisfied [with T = exprs::number]
expr(std::shared_ptr<T> e): ptr(std::static_pointer_cast<const expr_base>(std::move(e))) {}
expr.hpp
...
class expr;
class expr_base: public std::enable_shared_from_this<expr_base>
{
friend class expr;
protected:
expr_base() = default;
public:
using variable_map_t = std::map<std::string, double>;
virtual ~expr_base() = default;
};
class expr final {
private:
using const_pointer = std::shared_ptr<const expr_base>;
public:
using variable_map_t = expr_base::variable_map_t;
template <typename T, typename = std::enable_if_t<std::is_convertible<T*, const expr_base*>::value>>
expr(std::shared_ptr<T> e): ptr(std::static_pointer_cast<const expr_base>(std::move(e))) {}
expr() = default;
static expr number(double n);
operator const_pointer const &() const {return ptr;}
const expr_base* operator->() const {assert(ptr.get() != nullptr); return ptr.get();}
private:
const_pointer ptr;
};
expr.cpp
...
#include "expr.hpp"
#include "expr_impl.hpp"
expr expr::number(double n) {
return std::make_shared<exprs::number>(n); // It doesn't work
}
expr_impl.hpp
...
#include "expr.hpp"
namespace exprs {
class number:expr_base {
private:
double num_;
public:
number(double num): num_(num) {};
};
}
Consider the following snippet of code where I have a move-only Wrapper object that forwards the arguments to the underlying data on construction. Now if I use this Wrapper object inside another move-only class (SomeObject in this case), unless the move constructor for SomeObject is defaulted, it doesn't compile.
#include <utility>
template <typename T> class Wrapper {
public:
template <typename... Args>
Wrapper(Args &&... args) : _data{std::forward<Args>(args)...} {}
Wrapper(const Wrapper &) = delete;
Wrapper(Wrapper &&other) : _data(std::move(other)) {}
T &get() const { return _data; }
protected:
T _data;
};
struct SomeObject {
SomeObject(const SomeObject &) = delete;
SomeObject(SomeObject &&other) : x(std::move(other.x)) {}
//SomeObject(SomeObject &&other) = default; // this works!
SomeObject(int val) : x(val) {}
Wrapper<int> x;
};
int main() {
SomeObject obj(10);
return 0;
}
With gcc 6.3 we get:
test.cpp: In instantiation of ‘Wrapper<T>::Wrapper(Wrapper<T>&&) [with T = int]’:
test.cpp:20:56: required from here
test.cpp:10:52: error: cannot convert ‘std::remove_reference<Wrapper<int>&>::type {aka Wrapper<int>}’ to ‘int’ in initialization
Wrapper(Wrapper &&other) : _data(std::move(other)) {}
^
Is there something I am missing here? Aren't the user provided move constructor in SomeObject the same as the one the compiler would define when defaulted?
The only thing close to this that I could find is this answer but I feel like this is a different case since my move constructor for SomeObject isn't passing const types.
This is expected. Look at your code:
Wrapper(Wrapper &&other) : _data(std::move(other)) {}
T _data;
in your case, T is int. How do you want to initialize int from Wrapper<int>?
I'm trying to mimic the .Net implementation of a generic List in C++.
I've fleshed out the various interfaces as purely virtual abstract classes as follows:
template <typename T>
class ICollection {
public:
virtual void Add(T item) = 0;
virtual void Clear(void) = 0;
virtual bool Contains(T item) = 0;
virtual void Remove(T item) = 0;
virtual int32_t Count(void) const = 0;
};
template <typename T>
class IList : public ICollection<T> {
public:
virtual T Item(int32_t index) = 0;
virtual int32_t IndexOf(T item) = 0;
virtual void Insert(int32_t index, T item) = 0;
virtual void RemoveAt(int32_t index) = 0;
};
Now when I attempt to implement my main List class as follows:
template <typename T>
class List : public IList<T>, public ICollection<T> {
public:
List(void);
List(int32_ capacity);
// ICollection<T>
void Add(T item);
// other functions from ICollection
// IList<T>
T Item(int32_t index);
// other functions from IList
void AddRange(IList<T> items);
private:
typedef vector<T> ListType;
ListType *m_pList;
};
template <typename T>
List<T>::List(void) {
m_pList = new ListType();
}
template <typename T>
void List<T>::Insert(uint32_t index, T item) {
// Insert an entry into the list at the specified offset
m_list->insert(index, item);
}
// Implementation of other functions here...
As soon as I try to use the List<T> class as follows:
List<int32_t> myList;
A warning occurs saying:
In instantiation of 'class List<long int>':
required from here
warning: direct base 'ICollection<long int>' inaccessible in 'List<long int>' due to ambiguity [enabled by default]
class List : public IList<T>, public ICollection<T> {
^
Followed by the following error:
In instantiation of 'void List<T>::Insert(uint32_t, T) [with T = long int; uint32_t = long unsigned int]':
required from here
error: no matching function for call to 'std::vector<long int, std::allocator<long int> >::insert(uint32_t&, long int&)'
m_list->insert(index, item);
^
note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, const value_type&) [with _Tp = long int; _Alloc = std::allocator<long int>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<long int*, std::vector<long int, std::allocator<long int> > >; typename std::_Vector_base<_Tp, _Alloc>::pointer = long int*; std::vector<_Tp, _Alloc>::value_type = long int]
vector<_Tp, _Alloc>::
^
note: no known conversion for argument 1 from 'uint32_t {aka long unsigned int}' to 'std::vector<long int, std::allocator<long int> >::iterator {aka __gnu_cxx::__normal_iterator<long int*, std::vector<long int, std::allocator<long int> > >}'
If I modify the declaration of the List<T> class so as to remove the IList<T> and ICollection<T> abstract classes, no errors are generated.
I'm guessing that the way in which I'm using the templated base classes is not correct in this instance.
This issue is not directly related to templates.
class A {
public void f() {}
};
class B : public A {};
class C : public A, public B {};
int main() {
C c;
c.f(); // Error: ambiguous base class!
}
When you inherit a class, the derived class contains an object for the base class, called a base class subobject. So in my example, every B contains an A. And every C contains an A and a B. The problem is, when I try to call f as a member of C, the compiler needs to find the A subobject to call it on. But there are two subobjects with that type! One is directly inherited by C, and the other is inside the inherited B subobject. So the compiler can't figure out what I mean.
The solution in this case is to just not inherit a class twice. In my example, C doesn't need to directly inherit A, since inheriting B will provide it with an indirect A subobject and access to all its members.
In your case, List<T> doesn't need to inherit ICollection<T> directly. It's enough to just derive from IList<T>.
(In other cases, it can be useful to use "virtual inheritance", which tells the compiler "only create one base class subobject for this type, even if I indirectly inherit it more than once in some derived class". But that might be overkill for your code as it stands.)
all. I am trying to learn something about template inherit. I want to cast a temp derived object to its base reference. But I come across this problem:
typedef long size64_t;
//////Base class
template <typename _Scalar>
class MatrixBase{
public:
virtual _Scalar operator()(size64_t rowid, size64_t colid) = 0;
};
//////Derived class 1
template <typename _Scalar>
class MatrixHolder : public MatrixBase<_Scalar>{
public:
MatrixHolder(){};
inline _Scalar operator()(size64_t rowid, size64_t colid){return 0;}
};
//////Derived class 2
template <typename _Scalar>
class Matrix : public MatrixBase<_Scalar>{
public:
Matrix(){};
inline _Scalar operator()(size64_t rowid, size64_t colid){return 0;}
};
//////The function with parameters as Base class reference, wanting to get derived object as input.
template <typename _Scalar>
MatrixHolder<_Scalar> apply(MatrixBase<_Scalar>& lhs, MatrixBase<_Scalar>& rhs){
MatrixHolder<_Scalar> result;
return result;
}
and in main, we have:
void main(){
Matrix<double> m1;
Matrix<double> m2;
apply(m1, m2);//Sucess
apply(m1, apply(m1, m2));//Fail
}
the compiler said:
note: candidate function [with _Scalar = double] not viable: no known conversion from
'MatrixHolder<double>' to 'MatrixBase<double> &' for 2nd argument
MatrixHolder<_Scalar> apply(MatrixBase<_Scalar>& lhs, MatrixBase<_Scalar>& rhs){
^
1 error generated.
apply(m1, apply(m1, m2));//Fail
The problem here is that the inner apply returns a temporary, which cannot bind to the non-const reference parameter of the outer apply.
If you can make the parameters MatrixBase<_Scalar> const& it would be a possible match.
I'm learning boost and smart pointers. During compilation I got an error, and I can't figure out what is it about. I don't understand what I am doing wrong. The problem is in constructor:
DefaultCreature(const Creature& def) : def_(def) {}
Here is my code:
#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
class Creature;
typedef shared_ptr<Creature> PCreature;
class Creature {
public:
Creature(const string& name) : name_(name) {}
const string& getName() const { return name_; }
private:
string name_;
};
class DefaultCreature {
public:
DefaultCreature(const Creature& def) : def_(def) {}
private:
PCreature def_;
};
int main() {
DefaultCreature factory(Creature("lion"));
return 0;
}
And an error:
exercise1.cpp: In constructor ‘DefaultCreature::DefaultCreature(const Creature&)’:
exercise1.cpp:20:52: error: no matching function for call to ‘boost::shared_ptr<Creature>::shared_ptr(const Creature&)’
exercise1.cpp:20:52: note: candidates are:
In file included from /usr/local/include/boost/shared_ptr.hpp:17:0,
from /usr/local/include/boost/smart_ptr.hpp:21,
from exercise1.cpp:2:
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:472:14: note: template<class Ap> boost::shared_ptr::shared_ptr(Ap, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type)
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:472:14: note: template argument deduction/substitution failed:
/usr/local/include/boost/smart_ptr/shared_ptr.hpp: In substitution of ‘template<class Ap> boost::shared_ptr::shared_ptr(Ap, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type) [with Ap = Creature]’:
exercise1.cpp:20:52: required from here
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:472:14: error: no type named ‘type’ in ‘struct boost::detail::sp_enable_if_auto_ptr<Creature, int>’
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:446:14: note: template<class Y> boost::shared_ptr::shared_ptr(std::auto_ptr<_Tp1>&)
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:446:14: note: template argument deduction/substitution failed:
exercise1.cpp:20:52: note: types ‘std::auto_ptr<T>’ and ‘const Creature’ have incompatible cv-qualifiers
(...)
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:339:5: note: boost::shared_ptr<T>::shared_ptr() [with T = Creature]
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:339:5: note: candidate expects 0 arguments, 1 provided
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:328:25: note: boost::shared_ptr<Creature>::shared_ptr(const boost::shared_ptr<Creature>&)
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:328:25: note: no known conversion for argument 1 from ‘const Creature’ to ‘const boost::shared_ptr<Creature>&’
The argument to shared_ptr must be the address of a dynamically allocated object but the code is passing in a reference. Change to, for example:
class DefaultCreature {
public:
DefaultCreature(const Creature& def) : def_(new Creature(def)) {}
private:
PCreature def_;
};
or using boost::make_shared:
class DefaultCreature {
public:
DefaultCreature(const Creature& def) :
def_(boost::make_shared<Creature>(def)) {}
private:
PCreature def_;
};
If the instance of DefaultCreature is the only object that has access to the object being pointed to by def_ then there is no reason for it to be a boost::shared_ptr: use boost::scoped_ptr instead. See What C++ Smart Pointer Implementations are available? for a very useful overview of smart pointers.
However, from the posted code there appears to be no reason to be using pointers of any nature. Just store a Creature instance in DefaultCreature (Creature is copyable and there is no polymorphic requirement, based on the posted code).