I updated my C++ toolchain from Visual Studio 2013 to Visual Studio 2017/2019.
Now I am experiencing a number of compile errors in the form:
<source>(13): error C2280: 'OfflineFixture::OfflineFixture(const OfflineFixture &)': attempting to reference a deleted function
<source>(8): note: compiler has generated 'OfflineFixture::OfflineFixture' here
<source>(8): note: 'OfflineFixture::OfflineFixture(const OfflineFixture &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)'
A unique pointer is a member of class that has no constructor and destructor. In this case Visual Studio allows to instantiate an object this way:
OfflineFixture a{}; // works!
But using:
auto&& a = OfflineFixture{};
gives above compile error.
const auto& a = OfflineFixture{};
also gives above compile error.
Please have a look here: https://gcc.godbolt.org/z/XtP40t
My question is: Is my code wrong?
The given examples compiles using: gcc (9.1 and lower) clang Visual Studio 2013
But it fails in:
Visual Studio 2015
Visual Studio 2017
Visual Studio 2019
One way to fix this is to implement a default constructor in OfflineFixture.
Minimal example:
#include <memory>
struct OfflineFixture
{
void x() const {}
int i;
std::unique_ptr<int> m_calc;
};
int test() {
#if 1
const auto&& a = OfflineFixture{};
#else
OfflineFixture a{};
#endif
a.x();
return 0;
}