- Notifications
You must be signed in to change notification settings - Fork70
Closed
Description
Affected rules
- A7-1-1
Description
In the following code the member variablet_ is modified by the non-const member functionInit. If the class is not a template no error is reported for similar code.
Example
template<typename T>classA7_1_1bfinal {public:explicitA7_1_1b(std::int64_t i)noexcept { t_.Init(i); }// t_ is modified here by Initprivate: T t_;// <= A7-1-1: Non-constant variable t_ is used for an object and is not modified.};
Example
#include<iostream>#include<vector>namespacetest {/// doctemplate<classT>classMyClassfinal {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/// mainintmain(int,char**)noexcept {try { test::MyClass<int>c({1,2,3}); }catch (const std::exception&) { }}