jeudi 22 janvier 2015

Is there any difference between explicitly deleting a constructor vs. just not naming it?

In the following code, the compiler complains for ambiguity although one of the constructors is explicitly removed.



Minibox.cpp:26:16: error: call to constructor of 'Minibox<int>' is ambiguous
Minibox<int> b1 (a);
Minibox.cpp:14:3: note: candidate constructor has been explicitly deleted
Minibox (T a) = delete;
Minibox.cpp:16:12: note: candidate constructor
explicit Minibox (T & a) : value(a) {


But it goes fine just removing the line marked PROBLEM.



// g++ -std=c++11 Minibox.cpp
#include <iostream>

// ..................................................
template<typename T>
class Minibox {
public:
T value;

Minibox (T a) = delete; // PROBLEM

explicit Minibox (T & a) : value(a) {
std::cout << "T & a\n";
}
};

// ..................................................
int main () {
int a = 5;

Minibox<int> b1 (a);
}

Aucun commentaire:

Enregistrer un commentaire