Consider the following code:
#include <memory>
#include <vector>
class A
{
private:
std::vector<std::unique_ptr<int>> _vals;
};
int main()
{
A a;
//A a2(a);
return 0;
}
Compiler A compiles this without issue unless I uncomment out the line A a2(a);
at which point it complains about the copy constructor for std::unique_ptr
being deleted, and therefore I can't copy construct A
. Compiler B, however, makes that complaint even if I leave that line commented out. That is, compiler A only generates an implicitly defined copy constructor when I actually try to use it, whereas compiler B does so unconditionally. Which one is correct? Note that if I were to have used std::unique_ptr<int> _vals;
instead of std::vector<std::unique_ptr<int>> _vals;
both compilers correctly implicitly delete both copy constructor and assignment operator (std::unique_ptr
has a explicitly deleted copy constructor, while std::vector
does not).
(Note: Getting the code to compile in compiler B is easy enough - just explicitly delete the copy constructor and assignment operator, and it works correctly. That isn't the point of the question; it is to understand the correct behavior.)
class A
a DLL export), generates a copy constructor regardless of whether I need it. – Megdalclass A
as a DLL export (via, e.g.,class __declspec(dllexport) A
) that this happens. That might have been a clue to me... – Megdal/Qstd=c++11
(Intel Windows version ofstd=c++11
) – Megdal__declspec(dllexport)
, that should be part of the MCVE. (Especially if there is a chance that declaring a class for export causes its implicitly defined constructors to be instantiated for the DLL interface.) – Groove