C26414 issue with using CRecordset

270 Views Asked by At

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?

1

There are 1 best solutions below

2
On BEST ANSWER

There are two errors in your attempt to use auto for the class member m_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):

10.1.7.4 The auto specifier       [dcl.spec.auto]

1     The auto and decltype(auto) type-specifiers are used to designate a placeholder type that will be replaced later by deduction from an initializer. …

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. The auto 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:

std::unique_ptr<CRecordset> m_pRecords;