Document number P2657R0
Date 2022-10-11
Reply-to

Jarrad J. Waterloo <descender76 at gmail dot com>

Audience Evolution Working Group (EWG)

C++ is the next C++

Table of contents

Abstract

Programmer’s, Businesses and Government(s) want C++ to be safer and simpler. This has led some C++ programmers to create new programming languages or preprocessors, which again is a new language. This paper discusses using static analysis to make the C++ language itself safer and simpler.

Motivating Examples

Following is a wishlist. Most are optional. While, they all would be of benefit. It all starts with a new repeatable module level attribute that would preferably be applied once in the primary module interface unit and would automatically apply to it and all module implementation unit(s). It could also be applied to a module implementation unit but that would generally be less useful. However, it might aid in gradual migration.

export module some_module_name [[static_analysis("")]];// primary module interface unit
// or
module some_module_name [[static_analysis("")]];// module implementation unit

The name of the static analyzer are dotted. Unscoped or names that start with std., c++., cpp., cxx. or c. are reserved for standardization.

This proposal wants to stardardize two overarching static analyzer names; safer and modern.

[[static_analysis("safer")]]

The safer analyzer is for safety, primarily memory related. It is for those businesses and programmers who must conform to safety standards.

[[static_analysis("modern")]]

The safer analyzer is a subset of modern analyzer. The modern analyzer goes beyond just memory and safety concerns. It can be thought of as bleeding edge. It is for those businesses and programmers who commit to safety and higher quality modern code.

Neither is concerned about formatting or nitpicking. Both static analyzers only produce errors. These are meant for programmers, businesses and governments in which safety takes precedence. They both represent +∞. When a new version of the standard is released and adds new sub static analyzers than everyone’s code is broken, until their code is fixed. These sub static analyzers usually consist of features that have been mostly replaced with some other feature. It would be ideal if the errors produced not only say that the code is wrong but also provide a link to html page(s) maintained by the C++ teaching group, the authors of the C++ Core Guidelines [1] and compiler specific errors. These pages should provide example(s) of what is being replaced and by what was it replaced. Mentioning the version of the C++ standard would also be helpful.

All modules can be used even if they don’t use the static_analysis attribute as this allows gradual adoption.

What are the safer and modern analyzers composed of?

These overarching static analyzers are composed of multiple static analyzers which can be used individually to allow a degree of gradual adoption.

Use lvalue references

[[static_analysis("use_lvalue_references")]]

use_lvalue_references is a subset of safer.

WHY?

lvalue references

1985: Cfront 1.0 [2]

STL

1992 [2:1]

std::unique_ptr, std::shared_ptr, std::weak_ptr, std::reference_wrapper, std::make_shared

C++11

std::make_unique

C++14

std::string_view, std::optional, std::any, std::variant

C++17

std::make_shared support arrays, std::span

C++20

The C++ Core Guidelines [1:1] identifies issues that this feature helps to mitigate.

Gotchas

Usage of smart pointers

This static analyzer causes programmers to use 2 extra characters when using smart pointers, -> vs (*)., since the overloaded -> operator returns a pointer.

smart_pointer->some_function();// bad
(*smart_pointer).some_function();// good

the main function and environment variables

A shim module is needed in order to transform main and env functions into a more C++ friendly functions. These have been asked for years.

  1. A Modern C++ Signature for main [52]
  2. Desert Sessions: Improving hostile environment interactions [53]

No unsafe casts

[[static_analysis("no_unsafe_casts")]]

no_unsafe_casts is a subset of safer.

Why?

The C++ Core Guidelines [1:2] identifies issues that this feature helps to mitigate.


No unions

[[static_analysis("no_union")]]

no_union is a subset of safer.

It was replaced by std::variant, which is safer.

The C++ Core Guidelines [1:3] identifies issues that this feature helps to mitigate.


No mutable

[[static_analysis("no_mutable")]]

no_mutable is a subset of safer.

The programmer shall not lie to oneself. The mutable keyword violates the safety of const and is rarely used at a high level.


No new or delete

[[static_analysis("no_new_delete")]]

no_new_delete is a subset of safer.

It was replaced by std::make_unique and std::make_shared, which are safer.

The C++ Core Guidelines [1:4] identifies issues that this feature helps to mitigate.


No volatile

[[static_analysis("no_volatile")]]

no_volatile is a subset of safer.

The volatile keyword has nothing to do with concurrency. Use std::atomic or std::mutex instead.

The C++ Core Guidelines [1:5] identifies issues that this feature helps to mitigate.


No C style variadic functions

[[static_analysis("no_c_style_variadic_functions")]]

no_c_style_variadic_functions is a subset of safer.

C style variadic functions has been replaced by overloading, templates and variadic template functions.

The C++ Core Guidelines [1:6] identifies issues that this feature helps to mitigate.


No deprecated

[[static_analysis("no_deprecated")]]

no_deprecated is a subset of modern.

Deprecated functionality is not modern.


Use std::array

[[static_analysis("use_std_array")]]

use_std_array is a subset of modern.

Use std::array instead of C style/core C++ array.


What may safer and modern analyzers be composed of in the future?

No include

[[static_analysis("no_include")]]

no_include is a subset of modern.

The preprocessor directive #include has been replaced with import.
Don’t add the static analyzer until #embed is added.

NOTE: This may be impossible to implement as preprocessing occurs before compilation.


No goto

[[static_analysis("no_goto")]]

no_goto is a subset of modern.

Don’t add until break and continue to a label is added. Also a really easy to use finite state machine library may be needed.

The C++ Core Guidelines [1:7] identifies issues that this feature helps to mitigate.


Use std::function_ref

[[static_analysis("use_function_ref")]]

use_function_ref is a subset of modern.

Use std::function_ref instead of C style/core C++ [member] function pointers. std::function_ref can bind to stateful and stateless, free and member functions. It saves programmers from having to include a void* state parameter in their function pointer types and it also saves from having to include void* state parameter along side the function pointer type in each function where the function pointer type is used in function declarations. Neither of which could be performed with the "use_lvalue_references" static analyzer.

NOTE:


Summary

By adding static analysis to the C++ language we can make the language safer and easier to teach because we can restrict how much of the language we use. Human readable errors and references turns the compiler into a teacher freeing human teachers to focus on what the compiler doesn’t handle.

Frequently Asked Questions

Shouldn’t these be warnings instead of errors?

NO, otherwise we’ll be stuck with what we just have. C++ compilers produces plenty of warnings. C++ static analyzers produces plenty of warnings. However, when some one talks about creating a new language, then old language syntax becomes invalid i.e. errors. This is for programmers. Programmers and businesses rarely upgrade their code unless they are forced to. Businesses and Government(s) want errors, as well, in order to ensure code quality and the assurance that bad code doesn’t exist anywhere in the module. This is also important from a language standpoint because we are essentially pruning; somewhat. Keep in mind that all of these pruned features still have use now. In the future, as more constructs are built upon these pruned features, which is why they need to be part of the language, just not a part of everyday usage of the language.

Why at the module level? Why not safe and unsafe blocks?

Programmers and businesses rarely upgrade their code unless they are forced to. New programmers need training wheels and some of us older programmers like them too. Due to the proliferation of government regulations and oversight, businesses have acquired software composition analysis services and tools. These services map security errors to specific versions of modules; specifically programming artifacts such as executables and libraries. As such, businesses want to know if a module is reasonably safe.

How do we configure future analyzers?

Any arguments provided after the name of the analyzer can be forwarded onto the analyzer.

[[static_analysis("some_future_analyzer", 1, true, 0.5, "Hello World")]]

In this case, 1, true, 0.5, "Hello World" would all be forwarded to the static analyzer “some_future_analyzer”. None of the current analyzers use this functionality so this just illustrates distant future work where we can define these analyzers in a standard fashion but that can’t happen until we have a code DOM. As such, how these arguments are forwarded are currently compiler specific.

You must really hate pointers?

Actually, I love C, C++ and pointers.

The fact is pointers, unsafe casts, union, mutable and goto are the engine of C++ change. As such it would be foolish to remove them but it is also unrealistic for users/drivers of a vehicle to have to drive with nothing between them and the engine, without listening to them clamor for interior finishing.

C++ can’t standardize specific static analyzers

Do you fear that this could create a “subset of C++” that “could split the user community and cause acrimony”? [65]

First of all, let’s consider the quotes of Bjarne Stroustrup that this question are based upon.

“being defined by an ‘industry consortium.’ I am not in favor of language subsets or dialects. I am especially not fond of subsets that cannot support the standard library so that the users of that subset must invent their own incompatible foundation libraries. I fear that a defined subset of C++ could split the user community and cause acrimony” [65:1]

Does this paper create a subset? YES. Like it or not C++ already have a couple of subsets; some positive, some quasi. Freestanding is a subset for low level programming. This proposal primarily focus on high level programming but there is nothing preventing the creation of [[static_analysis("freestanding")]] which enforces freestanding. The C++ value categories has to some degree fractured the community into a clergy class that thoroughly understand its intracacies and a leity class that gleefully uses it.

Does this paper split the user community? YES and NO. It splits code into safer vs. less safe, high level vs. low level. However, this is performed at the module level, allowing the same programmer to decide what falls on either side of the fence. This would not be performed by an industry consortium but rather the standard. Safer modules can be used by less safe modules. Less safe modules can partly be used by safer modules, such as with the standard module. This latter impact is already minimalized because the standard frequently write their library code in C++ fashion instead of a C fashion.


“Are there any features you’d like to remove from C++?” [66]

Not really. People who ask this kind of question usually think of one of the major features such as multiple inheritance, exceptions, templates, or run-time type identification. C++ would be incomplete without those. I have reviewed their design over the years, and together with the standards committee I have improved some of their details, but none could be removed without doing damage. [66:1]

Most of the features I dislike from a language-design perspective (e.g., the declarator syntax and array decay) are part of the C subset of C++ and couldn’t be removed without doing harm to programmers working under real-world conditions. C++'s C compatibility was a key language design decision rather than a marketing gimmick. Compatibility has been difficult to achieve and maintain, but real benefits to real programmers resulted, and still result today. By now, C++ has features that allow a programmer to refrain from using the most troublesome C features. For example, standard library containers such as vector, list, map, and string can be used to avoid most tricky low-level pointer manipulation. [66:2]

The beauty of this proposal is it does not and it does remove features from C++. Like the standard library, it allows programmers to refrain from using the most troublesome C and C++ features.


“Within C++, there is a much smaller and cleaner language struggling to get out” [67]

Both making things smaller and cleaner requires removing something. When creating a new language, removing things happens extensively at the beginning but, frequently, features have to be added back in, when programmers clamor for them. This paper cleans up a programmers use of the C++ language, meaning less C++ has to be taught immediately, thus making things simpler. As a programmer matures, features can be gradually added to their repertoire, just as it was added to ours. After all, isn’t C++ larger now, than when we started programming in C++.

References


  1. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎

  2. https://en.cppreference.com/w/cpp/language/history ↩︎ ↩︎

  3. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#p4-ideally-a-program-should-be-statically-type-safe ↩︎

  4. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#p6-what-cannot-be-checked-at-compile-time-should-be-checkable-at-run-time ↩︎

  5. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#p7-catch-run-time-errors-early ↩︎

  6. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#p8-dont-leak-any-resources ↩︎

  7. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#p11-encapsulate-messy-constructs-rather-than-spreading-through-the-code ↩︎

  8. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#p12-use-supporting-tools-as-appropriate ↩︎

  9. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#p13-use-support-libraries-as-appropriate ↩︎

  10. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i4-make-interfaces-precisely-and-strongly-typed ↩︎

  11. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i11-never-transfer-ownership-by-a-raw-pointer-t-or-reference-t ↩︎

  12. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i12-declare-a-pointer-that-must-not-be-null-as-not_null ↩︎

  13. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i13-do-not-pass-an-array-as-a-single-pointer ↩︎

  14. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i23-keep-the-number-of-function-arguments-low ↩︎

  15. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f7-for-general-use-take-t-or-t-arguments-rather-than-smart-pointers ↩︎

  16. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f15-prefer-simple-and-conventional-ways-of-passing-information ↩︎

  17. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f22-use-t-or-ownert-to-designate-a-single-object ↩︎

  18. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f23-use-a-not_nullt-to-indicate-that-null-is-not-a-valid-value ↩︎

  19. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f25-use-a-zstring-or-a-not_nullzstring-to-designate-a-c-style-string ↩︎

  20. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f26-use-a-unique_ptrt-to-transfer-ownership-where-a-pointer-is-needed ↩︎ ↩︎

  21. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f27-use-a-shared_ptrt-to-share-ownership ↩︎ ↩︎

  22. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f42-return-a-t-to-indicate-a-position-only ↩︎

  23. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f43-never-directly-or-indirectly-return-a-pointer-or-a-reference-to-a-local-object ↩︎

  24. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c31-all-resources-acquired-by-a-class-must-be-released-by-the-classs-destructor ↩︎

  25. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c32-if-a-class-has-a-raw-pointer-t-or-reference-t-consider-whether-it-might-be-owning ↩︎

  26. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c33-if-a-class-has-an-owning-pointer-member-define-a-destructor ↩︎

  27. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c149-use-unique_ptr-or-shared_ptr-to-avoid-forgetting-to-delete-objects-created-using-new ↩︎ ↩︎

  28. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c150-use-make_unique-to-construct-objects-owned-by-unique_ptrs ↩︎ ↩︎

  29. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c151-use-make_shared-to-construct-objects-owned-by-shared_ptrs ↩︎ ↩︎

  30. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r1-manage-resources-automatically-using-resource-handles-and-raii-resource-acquisition-is-initialization ↩︎

  31. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r2-in-interfaces-use-raw-pointers-to-denote-individual-objects-only ↩︎

  32. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r3-a-raw-pointer-a-t-is-non-owning ↩︎

  33. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r5-prefer-scoped-objects-dont-heap-allocate-unnecessarily ↩︎

  34. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r10-avoid-malloc-and-free ↩︎

  35. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r11-avoid-calling-new-and-delete-explicitly ↩︎ ↩︎

  36. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r12-immediately-give-the-result-of-an-explicit-resource-allocation-to-a-manager-object ↩︎

  37. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r13-perform-at-most-one-explicit-resource-allocation-in-a-single-expression-statement ↩︎

  38. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r14-avoid--parameters-prefer-span ↩︎

  39. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r15-always-overload-matched-allocationdeallocation-pairs ↩︎

  40. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r20-use-unique_ptr-or-shared_ptr-to-represent-ownership ↩︎ ↩︎

  41. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r22-use-make_shared-to-make-shared_ptrs ↩︎ ↩︎

  42. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r23-use-make_unique-to-make-unique_ptrs ↩︎ ↩︎

  43. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es20-always-initialize-an-object ↩︎

  44. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es24-use-a-unique_ptrt-to-hold-pointers ↩︎

  45. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es42-keep-use-of-pointers-simple-and-straightforward ↩︎

  46. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es47-use-nullptr-rather-than-0-or-null ↩︎

  47. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es60-avoid-new-and-delete-outside-resource-management-functions ↩︎ ↩︎

  48. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es61-delete-arrays-using-delete-and-non-arrays-using-delete ↩︎ ↩︎

  49. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es65-dont-dereference-an-invalid-pointer ↩︎

  50. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#e13-never-throw-while-being-the-direct-owner-of-an-object ↩︎

  51. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cpl1-prefer-c-to-c ↩︎

  52. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0781r0.html ↩︎

  53. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1275r0.html ↩︎

  54. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c146-use-dynamic_cast-where-class-hierarchy-navigation-is-unavoidable ↩︎

  55. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es48-avoid-casts ↩︎

  56. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es49-if-you-must-use-a-cast-use-a-named-cast ↩︎

  57. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es50-dont-cast-away-const ↩︎

  58. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c181-avoid-naked-unions ↩︎

  59. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cp8-dont-try-to-use-volatile-for-synchronization ↩︎

  60. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f55-dont-use-va_arg-arguments ↩︎

  61. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#-es34-dont-define-a-c-style-variadic-function ↩︎

  62. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es76-avoid-goto ↩︎

  63. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2472r3.html ↩︎

  64. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0792r11.html ↩︎

  65. https://www.stroustrup.com/bs_faq.html#EC++ ↩︎ ↩︎

  66. https://www.stroustrup.com/bs_faq.html#remove-from-C++ ↩︎ ↩︎ ↩︎

  67. https://www.stroustrup.com/quotes.html ↩︎