Effective C++ (Myers has nothing to do with it :). The C++ ecosystem is still rapidly evolving, which makes this language one of the most effective tools of our time. I'd like to point out three reasons why C++ is so attractive nowadays. The first is the new extensions to the language standard that enable programmers to write effective and efficient code. The second is the maturity of the development tools and acceleration of the code building process. The third is the maturity of the auxiliary tools allowing us to keep control over the code quality and other aspects of the software development life cycle. This talk is an ode to the C++ language!
2. •IBM RPG is a programming
language with a syntax that was
originally similar to the command
language of IBM mechanical
tabulating machines
•It was widely used in 1960s and
1970s
IBM 1401
Are C and C++ alive? Even IBM RPG is still
alive!
3. break in switch: problem
....
case ADDRESS_HOME_LINE3:
group = GROUP_ADDRESS_LINE_3;
break;
case ADDRESS_HOME_STREET_ADDRESS:
group = GROUP_STREET_ADDRESS;
case ADDRESS_HOME_CITY:
group = GROUP_ADDRESS_CITY;
break;
case ADDRESS_HOME_STATE:
group = GROUP_ADDRESS_STATE;
break;
....
Chromium
4. break in switch: huge problem
• Qt
• GDB
• Redis
• EA WebKit
• Unreal Engine 4
• Chromium
• ....
5. break in switch: interim solutions
• MISRA: a non-empty case should always be completed by
the operator break
• [[gnu::fallthrough]]
• [[clang::fallthrough]]
• __attribute__((fallthrough))
• BOOST_FALLTHROUGH
6. break в switch: [[fallthrough]]
switch (i)
{
case 10:
f1();
break;
case 20:
f2();
break;
case 30:
f3();
[[fallthrough]]; // The warning will be suppressed
case 40:
f4();
break;
Clang, GCC: -Wimplicit-fallthrough
7. C++17: features that were removed
• Trigraphs were removed
• The register keyword cannot be used as a variable
specifier
• Prefix and postfix increments for bool type were
removed
• V552 diagnostic in PVS-Studio analyzer will become irrelevant
• std::auto_ptr was removed, std::unique_ptr should
be used instead
??= #
??/
??' ^
??( [
??) ]
??! |
??< {
??> }
??- ~
8. Non-constant string::data
• In C++17, std::string now has the data() method, which
returns a non-constant pointer to the internal data of a
string.
• Now I believe that we are really being taken care of. A
PRACTICAL problem is actually solved.
It wasn’t nice to write &str.front() and &str[0]
std::string str = "hello";
char *p = str.data();
p[0] = 'H';
std::cout << str << 'n'; // Hello
10. A custom string class is normal
• Sooner or later, any decent project
obtains its own string class.
• I've been waiting for it to appear in PVS-
Studio.
• It appeared in our project and it was a
justified decision.
• It is normal. Feel free to do it.
From my report for C++ Russia 2016
11. std::string
• C++ Russia 2017: Anton Polukhin «What not to do: C++
wheel reinventing for professionals» (RU)
https://youtu.be/rJWSSWYL83U
• vstring
• std::string
12. Constexpr if
template <typename T>
auto GetValue(T t)
{
if constexpr (std::is_pointer<T>::value)
{
return *t;
}
else
{
return t;
}
}
void foo()
{
int v = 10;
std::cout << GetValue(v) << 'n'; // 10
std::cout << GetValue(&v) << 'n'; // 10
}
13. Initializer in if and switch
if ((len=input.find("length=",start)!=std::string::npos))
length=atoi(&(input.c_str()[len+strlen("length=")]));
SETI@home
14. Initializer in if and switch
if (auto it = m.find(key); it != m.end())
{
....
}
19. Static code analyzers
• Coverity
• Klocwork
• Parasoft
• PVS-Studio
• SonarQube
• "Legion is their name": List of tools for static code analysis
https://en.wikipedia.org/wiki/List_of_tools_for_static_code_
analysis
20. Modern C++ doesn’t eliminate the need for
static analysis
TDLib (C++)void FileGcWorker::run_gc(....,
std::vector<FullFileInfo> files, ....)
{
....
std::sort(files.begin(), files.end(),
[](const auto &a, const auto &b)
{
return a.atime_nsec < a.atime_nsec;
});
....
}
21. Static analysis isn’t a patch for C and C++
static bool AreEqual( VisualStyleElement value1,
VisualStyleElement value2)
{
return
value1.ClassName == value1.ClassName &&
value1.Part == value2.Part &&
value1.State == value2.State;
}
Mono (C#)
22. PVS-Studio
• I would like to introduce Mikhail Matrosov
• Technical manager in the Moscow R&D Office of the Align Technology
company
23. General information
• Align Technology R&D
• 1.5M LOC C++
• Including a very dusty legacy :)
• 150 commits per day
• 5 developers per month :)
24. Automation
• CI Server (Bamboo)
• Daily build for the incremental analysis (approx. 15 minutes)
• Night build for full analysis (approx. 6 hours)
• L3 are not checked
25. Process from a developer's perspective
• Upload the changes to trunk
• On the CI server, a build with incremental analysis is automatically
started
• If the analysis finds PVS-Studio violations, I get a notice
• Correct the violations. I can check locally. Upload.
26. Process from the implementer’s point of view
• Every morning I check the results of a full analysis
• If there are violations, it means that either
• someone of developers missed a notification, in this case, I write a reminding
letter to him, or
• something went wrong in the process of the analysis, or notification wasn’t
sent, in this case, I investigate the issue
• If a developer often misses notifications, I have an educational
conversation with him
27. Deployment
• Full analysis of the sources
• Reviewing each rule - to which extent it applies to our codebase
• If a rule gives too many false positives, it is removed from the analysis using
the .pvsconfig file
• A small number of the most terrible violations must be fixed
immediately
• Suppress all other violations
28. Overall impression
• The tool is quite simple in deployment and configuration
• Helped to find many real problems. The process is established and
works well enough.
• The authors quickly respond and quickly fix the problems. Can
implement a feature on request.
• The analyzer is often mistaken on complex C++ code. Lack of reliable
parser (clang) has its impact. There is a feeling that this will not
improve. At the moment it seems that duck tape is used when
needed.
30. Acceleration of build: compiler cache
• When compiling a preprocessed file on the basis of its
contents, compiler flags and the output from the compiler,
the hash value is calculated
• If you recompile an unchanged file with the same flags, a
ready-made object file will be taken out from the cache and
fed to the linker input
35. Profiling tools of C++ code
• As the saying goes, let me "pass by reference" your attention to this
report.
• Alexander Zaitsev. Profiling tools of C++ code.
Minsk, CoreHard C++ Conference in Spring 2018
• The video will become available on YouTube in 3 months
36. and so on
• C++ Core Guidelines
• Community, conferences
• The job market