Closed
Description
Affected rules
- A7-1-1
Description
In the following code the member variable t_
is modified by the non-const member function Init
. If the class is not a template no error is reported for similar code.
Example
template<typename T>
class A7_1_1b final {
public:
explicit A7_1_1b(std::int64_t i) noexcept { t_.Init(i); } // t_ is modified here by Init
private:
T t_; // <= A7-1-1: Non-constant variable t_ is used for an object and is not modified.
};
Example
#include <iostream>
#include <vector>
namespace test {
/// doc
template <class T>
class MyClass final {
private:
// CodeQL reports: A7-1-1:cpp/autosar/declaration-unmodified-object-missing-const-specifier
// Non-constant variable data_ is used for an object and is not modified.
// Marking this as "const" is incorrect, we can modify the data through the iterator returned
// by the begin() function, adding const here does not compile.
std::vector<T> data_;
};
} // namespace test
/// main
int main(int, char**) noexcept {
try {
test::MyClass<int> c({1, 2, 3});
} catch (const std::exception&) {
}
}