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 expressionpN->getParent()
, which returns a typeunique_ptr<node<T>>&.
So I am not sure about the source of this error.
Could anyone point to the same?
auto
uses) correctly, the original type,P
, has referenceness stripped when determining the deduced type,A
(see item #3 after "Before deduction begins, the following adjustments to P and A are made:", which states "If P is a reference type, the type referred to by P is used for deduction."). Basically,auto
means value type unless you explicitly make itauto&&
orauto&
. Usingconst auto&
(and maybeauto&&
; my C++ is rusty) should work. – ShadowRanger...
are not good to describe a good problem to be StackOverflow solved. Please, illustrate your example with a complete, verifiable and reproducible code. Also, in the question, you refer to a deleted copy constructor, thing that cannot happen, you can delete an instance, or several, but never the copy constructor.... vage questions demand vague responses.... please, correct that. – Luis Colorado