Reputation:
I have two template declarations as follows:
template < typename T >
class node {
public:
...
const unique_ptr < node < T >> & getParent();
...
private:
...
unique_ptr < node < T >> & P; //for parent node
...
};
template < typename T1, typename T2 >
class tree {
public:
tree(int, T2 & );
...
void travPreord();
...
private:
...
};
Within the definition of the public method
template<typename T1, typename T2> tree<T1,T2>::travPreord()
I have the following code:
template < typename T1, typename T2 >
void tree < T1, T2 > ::travPreord() {
//lambda definition
function < void(unique_ptr < node < T1 >> & ) > prntNode = [ & ]
(unique_ptr < node < T1 >> & pN) {
...
if(auto rPN = pN - > getParent()) {
...
}
};
}
For the assignment inside the if statement condition above, I get the following error from the compiler (g++ 4.2.1):
error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr >, std::__1::default_delete > > >' if(auto rPN = pN->getParent()){
The arguments to the template instantiation highlighted in the error are supplied from the main():
int main() {
vector < string > v;
...
tree < string, vector < string >> T(v.size(), v);
T.travPreord();
...
return 0;
}
I wrote the if statement condition under question on the following assumptions:
It is possible to assign a unique_ptr to a reference.
The auto keyword should deduce the type of the lvalue expression rPN
to be the type of the rvalue expression pN->getParent()
, which returns a type unique_ptr<node<T>>&.
So I am not sure about the source of this error.
Could anyone point to the same?
Upvotes: 1
Views: 4966
Reputation: 172924
- The auto keyword should deduce the type of the lvalue expression
rPN
to be the type of the rvalue expressionpN->getParent()
, which returns a typeunique_ptr<node<T>>&
.
No, the type will be unique_ptr<node<T>>
.
Auto type deduction applies same rules as template argument deduction; the reference part of the expression (i.e. pN->getParent()
) is ignored, after that the const
part is ignored too. So the result type is unique_ptr<node<T>>
.
You need to specify that it's should be a reference explicitly, e.g.
auto& rPN = pN - > getParent() // rPN is of type const unique_ptr<node<T>>&
- It is possible to assign a unique_ptr to a reference.
Of course yes. The problem is that rPN
is not a reference.
Upvotes: 3