Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[onert] Apply c++17 features #14647

Closed
ragmani opened this issue Feb 12, 2025 · 16 comments
Closed

[onert] Apply c++17 features #14647

ragmani opened this issue Feb 12, 2025 · 16 comments

Comments

@ragmani
Copy link
Contributor

ragmani commented Feb 12, 2025

What

Let's apply C++17 features to onert. The goal is to modernize the codebase by leveraging new language features, thereby simplifying the code and improving maintainability.

Why

  • Simplification: Modern C++17 features can reduce boilerplate code and improve code readability.
  • Maintainability: Using updated syntax and constructs makes it easier to maintain and extend the code in the future.
  • Performance & Safety: Some C++17 features can lead to better performance and more robust error handling.
  • Consistency: Standardizing the codebase to a modern C++ standard promotes consistency across the project.

Features to be applied

  • Nested Namespace Declarations

    • Simplifies deeply nested namespace syntax, reducing verbosity and improving readability.
    • cppreference
  • Inline Variables

    • Avoids multiple definition issues by allowing variables to be defined in headers without violating ODR.
    • cppreference
  • Structured Bindings

    • Provides a concise way to unpack tuples or struct members, making code cleaner and easier to understand.
    • cppreference
  • if constexpr

    • Enables compile-time conditional branching in templates, reducing code complexity and avoiding unnecessary instantiations.
    • cppreference
  • Fold Expressions

    • Simplifies the handling of variadic templates by reducing parameter packs with a single expression.
    • cppreference
  • Template Argument Deduction for Class Templates (Deduction Guides)

    • Eliminates the need to specify template arguments explicitly when constructing objects, enhancing code brevity.
    • cppreference
  • Filesystem Library (std::filesystem)

    • Standardizes file and directory operations, making code more portable and easier to maintain compared to platform-specific APIs.
    • cppreference
  • std::optional

    • Offers a safer alternative to pointers or sentinel values for representing optional data, improving code clarity and robustness.
    • cppreference
  • std::variant

    • Provides a type-safe union for handling multiple types in a single variable, enhancing code safety and flexibility.
    • cppreference
  • std::any

    • Enables storage of any type in a type-safe manner, useful for heterogeneous collections without compromising type safety.
    • cppreference
  • std::string_view

    • Allows efficient non-owning views over strings, reducing unnecessary copying and improving performance when handling substrings.
    • cppreference
  • Parallel Algorithms (Execution Policies)

    • Facilitates the parallelization of standard algorithms, potentially improving performance on multi-core systems with minimal code changes.
    • cppreference
  • [[nodiscard]] Attribute

    • Helps prevent bugs by issuing warnings when important return values are ignored, ensuring that critical results are handled properly.
    • cppreference
  • std::void_t

    • This utility simplifies SFINAE (Substitution Failure Is Not An Error) in template metaprogramming. It can be used to detect the presence of member types or functions in a concise way.
  • std::uncaught_exceptions()

    • This function returns the number of exceptions that have been thrown but not yet caught. It can improve exception-safety code by providing a more robust way to detect if an exception is active compared to the older std::uncaught_exception().
  • std::shared_mutex

    • C++17 introduces std::shared_mutex (and std::shared_lock) which allow multiple readers or one writer at a time. This can be
      particularly useful in multithreaded code where you have many concurrent read operations.
  • std::not_fn

    • This function adaptor returns a callable that performs the logical NOT on the result of another callable. It can simplify certain functional programming patterns, such as filtering or negating predicates.
  • std::byte

    • Provides a type-safe way to work with raw byte data instead of using char or unsigned char. This can improve code clarity and help avoid mistakes in low-level memory manipulation.
  • Type Trait Enhancements

    • C++17 adds variable templates and logical operator type traits (like std::bool_constant) to simplify template metaprogramming and make type trait expressions clearer and more concise.
@ragmani
Copy link
Contributor Author

ragmani commented Feb 13, 2025

@ragmani
Copy link
Contributor Author

ragmani commented Feb 13, 2025

if constexpr

@ragmani
Copy link
Contributor Author

ragmani commented Feb 14, 2025

@ragmani
Copy link
Contributor Author

ragmani commented Feb 14, 2025

Template Argument Deduction for Class Templates (Deduction Guides)

This feature could negatively impact readability, so I will not be applying it.

@ragmani
Copy link
Contributor Author

ragmani commented Feb 14, 2025

Filesystem Library (std::filesystem)

This feature was already resolved by #12450

@ragmani
Copy link
Contributor Author

ragmani commented Feb 14, 2025

std::optional

Considering consistency with the existing code and potential performance overhead, I did not find any suitable places to apply std::optional.

@ragmani
Copy link
Contributor Author

ragmani commented Feb 14, 2025

@ragmani
Copy link
Contributor Author

ragmani commented Feb 17, 2025

@ragmani
Copy link
Contributor Author

ragmani commented Feb 18, 2025

Parallel Algorithms (Execution Policies)

According to https://en.cppreference.com/w/cpp/compiler_support/17, neither GCC nor Clang fully support this feature. Also, according to https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017:

Note 3: The Parallel Algorithms have an external dependency on Intel TBB 2018 or later. If the header is included then -ltbb must be used to link to TBB.

This means that when using parallel algorithms in C++17, the feature relies on Intel TBB 2018 (or later), and you must link with TBB (using the -ltbb flag) if you include the header.

@ragmani
Copy link
Contributor Author

ragmani commented Feb 19, 2025

[[nodiscard]] Attribute

@ragmani
Copy link
Contributor Author

ragmani commented Feb 19, 2025

Type Trait Enhancements

@glistening
Copy link
Contributor

glistening commented Feb 24, 2025

I am not sure all items of the listed c++17 pays or valuable.

  • Simplification: Modern C++17 features can reduce boilerplate code and improve code readability.
  • Maintainability: Using updated syntax and constructs makes it easier to maintain and extend the code in the future.
  • Performance & Safety: Some C++17 features can lead to better performance and more robust error handling.
  • Consistency: Standardizing the codebase to a modern C++ standard promotes consistency across the project.

For example, using std::byte is more simple? more maintainable? give more performance? consistency? I don't think so.

We may choose the most effective items selectively.

@ragmani
Copy link
Contributor Author

ragmani commented Feb 24, 2025

Not every C++17 feature is a silver bullet for every project. The bullet points are general benefits that may apply in many cases, but the actual impact depends on your codebase and use case. For instance:

  • Simplification: Some features reduce boilerplate code and make your intent clearer. However, not every feature will simplify every piece of code. For example, std::byte is intended to provide a type-safe way of dealing with raw byte data, which can improve clarity in low-level code, but it might not simplify high-level logic.

  • Maintainability: Modern constructs can sometimes make the code easier to read and maintain, especially if your team is familiar with newer idioms. Yet, if a feature doesn't fit your existing style or if its benefits are marginal, it might not significantly affect maintainability.

  • Performance & Safety: Certain features (like parallel algorithms or noexcept) can help optimize performance or ensure robust error handling. But many features, including std::byte, primarily serve to clarify intent rather than directly improve performance.

  • Consistency: Standardizing on a modern C++ standard can promote a consistent coding style across the project. Still, you have the flexibility to adopt only those features that offer tangible benefits for your specific context.

In summary, we don't have to adopt every C++17 feature blindly. We can evaluate each one based on its impact on our project's simplicity, maintainability, performance, safety, and consistency—and choose the most effective ones selectively.

@ragmani
Copy link
Contributor Author

ragmani commented Mar 6, 2025

I'm closing this issue as I think I have applied a sufficient number of C++ features, even though I didn't aim to cover all of them.

@ragmani ragmani closed this as completed Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants