I have seen a similar discussion here (C++ Code Analysis in Visual Studio Community 2019 produces warnings C26486 and C26414) where it refers to using std::move
but I am not sure if this is what I need to do.
I turned CRecordset *pRecords = new CRecordSet(&m_dbDatabase)
into smart pointers. Like this:
auto pRecordset = std::make_unique<CRecordset>(&m_dbDatabase);
Now I get 8 similar code analysis warnings:
C26414: Move, copy, reassign or reset a local smart pointer 'pRecords' (r.5).
I think I understand that fact that this could be turned into a member variable of my CPTSDatabase
class, that is populated when the database is opened. And then simply use the same pointer.
In an attempt to try and do that I added this to my header file:
auto m_pRecords = std::unique_ptr<CRecordSet>;
Then I was going to do this after opening the database:
m_pRecords = std::make_unique<CRecordSet>(&m_dbDatabase);
But it does not like how I defined the variable.
How do I resolve this?
There are two errors in your attempt to use
auto
for the class memberm_pRecords
.First, you can't use
auto
for (non-static) class member variables. I can't find an explicit rule for this in this C++17 Draft Standard, but the very first part of the [dcl.spec.auto] section does, indirectly, cover it (emphasis mine):So, when defining a class, that later deduction is not appropriate, as the class isn't fully defined from the information you provide.
And second, even outside a class definition, the
auto m_pRecords = std::unique_ptr<CRecordset>;
expression is invalid syntax. Theauto
keyword allows a variable's type to be deduced from the value of the expression given as its initialisation (the right-hand operator of the=
) – and a type name is not an expression.So, as a solution, you should declare the smart-pointer member of your class like this: