top of page

Parent teacher association

공개·회원 7명

Download Exceptional C++ Style PDF for Free and Improve Your C++ Programming Skills



Exceptional C++ Style: A Guide for C++ Programmers




C++ is a powerful, versatile, and complex programming language that offers many benefits but also many pitfalls. To write high-quality, maintainable, and reliable C++ code, you need to follow some best practices and guidelines that can help you avoid common errors, improve performance, and enhance readability. This is what exceptional C++ style is all about.




exceptional c style pdf download


DOWNLOAD: https://www.google.com/url?q=https%3A%2F%2Ftinourl.com%2F2udc9s&sa=D&sntz=1&usg=AOvVaw3Z4CC6XAzi5vTMz4UWJ-Lu



In this article, you will learn what exceptional C++ style is, how to write exceptional C++ style code, and where to learn more about it. You will also find some frequently asked questions about exceptional C++ style at the end of the article.


What is Exceptional C++ Style?




Exceptional C++ style is a term coined by Herb Sutter, a prominent C++ expert and author of several books on the topic. Exceptional C++ style refers to a set of principles, guidelines, techniques, and idioms that can help you write better C++ code that is more robust, efficient, elegant, and consistent.


Some of the benefits of exceptional C++ style are:


  • It can help you avoid common mistakes and bugs that can lead to crashes, memory leaks, undefined behavior, or security vulnerabilities.



  • It can help you improve the performance and scalability of your code by using appropriate data structures, algorithms, memory management, and concurrency features.



  • It can help you enhance the readability and maintainability of your code by using clear names, consistent formatting, proper documentation, and modular design.



  • It can help you leverage the full potential of modern C++ features such as templates, exceptions, smart pointers, containers, algorithms, lambda expressions, etc.



  • It can help you write code that is portable across different platforms, compilers, and standards.



How to Write Exceptional C++ Style Code?




There is no definitive or authoritative answer to this question, as different programmers may have different opinions and preferences on how to write exceptional C++ style code. However, there are some general principles and guidelines that are widely accepted and recommended by many C++ experts. Here are some of them:


Use const Correctly




The const keyword in C++ has two meanings: it can indicate that a variable or a parameter is not modifiable (constness), or it can indicate that a member function does not modify the state of an object (const-correctness). Using const correctly can have several advantages:


  • It can prevent accidental or intentional modification of data that should not be changed.



  • It can enable compiler optimizations and type checking.



  • It can improve the readability and expressiveness of your code by making your intentions clear.



  • It can facilitate the use of const references, which can avoid unnecessary copying and improve performance.



Some examples of using const correctly are:


// Declare a constant variable const int PI = 3.14; // Pass a parameter by const reference void print(const std::string& s); // Declare a const member function class Point public: // ... double distance(const Point& p) const; ;


Prefer References to Pointers




A reference in C++ is an alias for another object, while a pointer is a variable that stores the address of another object. References and pointers have different syntax and semantics, and they have different trade-offs. In general, you should prefer references to pointers when possible, because:


  • References are safer and easier to use than pointers, as they cannot be null, uninitialized, or dangling.



  • References are more expressive and concise than pointers, as they do not require dereferencing or checking.



  • References can avoid memory leaks and ownership issues that pointers may cause.



Some examples of preferring references to pointers are:


// Return a reference instead of a pointer std::string& get_name(); // Use a reference instead of a pointer in a loop for (auto& x : v) // ... // Use a reference instead of a pointer in a lambda expression auto f = [&]() // ... ;


Avoid Memory Leaks and Dangling Pointers




A memory leak occurs when dynamically allocated memory is not freed properly, resulting in wasted resources and potential performance degradation. A dangling pointer occurs when a pointer points to an invalid or deallocated memory location, resulting in undefined behavior and potential crashes. To avoid memory leaks and dangling pointers, you should:


  • Use the new and delete operators carefully and consistently, and always match them in pairs.



  • Use RAII (Resource Acquisition Is Initialization) technique, which ensures that resources are acquired in constructors and released in destructors.



  • Use smart pointers instead of raw pointers, as they can manage the memory allocation and deallocation automatically.



  • Avoid returning pointers or references to local variables or temporary objects, as they will go out of scope after the function returns.



Some examples of avoiding memory leaks and dangling pointers are:


// Use RAII technique class File public: File(const std::string& name) f = fopen(name.c_str(), "r"); File() fclose(f); private: FILE* f; ; // Use smart pointers std::unique_ptr p(new int(42)); std::shared_ptr q(new std::string("hello")); // Avoid returning pointers or references to local variables or temporary objects int* bad_function() int x = 10; return &x; // bad: x will go out of scope std::string& worse_function() std::string s = "world"; return s; // worse: s will go out of scope


Use Smart Pointers Wisely




A smart pointer is a class that wraps a raw pointer and provides additional features such as automatic memory management, reference counting, ownership transfer, etc. C++ provides several types of smart pointers in the standard library, such as std::unique_ptr, std::shared_ptr, std::weak_ptr, etc. Using smart pointers wisely can help you avoid memory leaks and dangling pointers, but you should also be aware of some caveats:


  • Choose the right type of smart pointer for your use case. For example, use std::unique_ptr for exclusive ownership, use std::shared_ptr for shared ownership, use std::weak_ptr for breaking cycles, etc.



  • Avoid using smart pointers for arrays, as they do not support array operations such as indexing or iteration. Use std::vector or std::array instead.



  • Avoid using smart pointers for polymorphic objects, as they do not support virtual functions or dynamic_cast. Use std::unique_ptr with custom deleters or std::shared_ptr with std::enable_shared_from_this instead.



  • Avoid mixing smart pointers with raw pointers, as they may cause double deletion or memory corruption. Use std::make_unique or std::make_shared to create smart pointers from raw pointers.



Some examples of using smart pointers wisely are:


type of smart pointer for your use case std::unique_ptr a(new Dog()); // good: exclusive ownership std::shared_ptr b(new Cat()); // good: shared ownership std::weak_ptr c(b); // good: breaking cycles // Avoid using smart pointers for arrays std::unique_ptr x(new int[10]); // bad: no array operations std::vector y(10); // good: supports array operations // Avoid using smart pointers for polymorphic objects std::unique_ptr s(new Circle()); // bad: no virtual functions s->draw(); // bad: calls Shape::draw instead of Circle::draw std::unique_ptr t(new Triangle(), [](Shape* p) delete p; ); // good: custom deleter // Avoid mixing smart pointers with raw pointers std::shared_ptr p(new int(42)); // bad: may cause double deletion std::shared_ptr q(p.get()); // bad: may cause double deletion std::shared_ptr r = std::make_shared(42); // good: no raw pointer involved


Use Exceptions for Error Handling




An exception is an abnormal event that occurs during the execution of a program, such as division by zero, invalid input, file not found, etc. C++ provides a mechanism for throwing and catching exceptions, which can help you handle errors in a graceful and consistent way. Using exceptions for error handling can have several advantages:


  • It can separate the normal logic from the error handling logic, making your code more readable and maintainable.



  • It can propagate errors across function calls without requiring explicit return values or error codes.



  • It can provide more information and context about the error, such as the type, message, and location of the exception.



  • It can allow you to handle different types of errors in different ways, using polymorphism and inheritance.



Some examples of using exceptions for error handling are:


// Throw an exception when an error occurs void divide(int x, int y) if (y == 0) throw std::runtime_error("Division by zero"); // ... // Catch an exception when it is thrown try divide(10, 0); catch (const std::exception& e) std::cerr


Write Exception-Safe Code




Exception-safe code is code that does not cause any unwanted side effects when an exception is thrown, such as memory leaks, resource leaks, data corruption, or inconsistent state. Writing exception-safe code is important for ensuring the correctness and reliability of your program. To write exception-safe code, you should follow some principles and techniques:


  • Use RAII technique to manage resources automatically.



  • Use smart pointers to manage memory automatically.



  • Use standard library containers and algorithms to manage data automatically.



  • Avoid throwing exceptions in constructors and destructors.



  • Avoid throwing exceptions from overloaded operators.



  • Avoid catching exceptions by value or by pointer.



  • Avoid swallowing or ignoring exceptions.



Some examples of writing exception-safe code are:


// Use RAII technique to manage resources automatically class Lock public: Lock(Mutex& m) : mutex(m) mutex.lock(); Lock() mutex.unlock(); private: Mutex& mutex; ; // Use smart pointers to manage memory automatically void foo() std::unique_ptr p(new int(42)); // ... throw std::runtime_error("Something went wrong"); // p will be deleted automatically // Use standard library containers and algorithms to manage data automatically void bar() std::vector v(10); // ... throw std::runtime_error("Something went wrong"); // v will be destroyed automatically // Avoid throwing exceptions in constructors and destructors class Foo public: Foo() // do not throw exceptions here Foo() // do not throw exceptions here ; // Avoid throwing exceptions from overloaded operators class Bar public: // ... Bar& operator=(const Bar& other) // do not throw exceptions here return *this; ; // Avoid catching exceptions by value or by pointer try // ... catch (std::exception e) // bad: slicing may occur // ... catch (std::exception* e) // bad: memory management may be unclear // ... // Avoid swallowing or ignoring exceptions try // ... catch (...) // bad: do not catch all exceptions and do nothing


Use Templates for Generic Programming




A template is a feature in C++ that allows you to write code that can work with different types of data, without requiring duplication or type casting. Templates can be used to define generic functions, classes, variables, or aliases that can be instantiated with different types of arguments. Using templates for generic programming can have several advantages:


  • It can increase the reusability and maintainability of your code, as you can write one template that can work with many types.



  • It can improve the performance and efficiency of your code, as templates are resolved at compile time and avoid runtime overhead.



  • It can enhance the type safety and correctness of your code, as templates can enforce compile-time checks and avoid implicit conversions.



  • It can enable the use of advanced techniques such as metaprogramming, which can generate code at compile time based on template parameters.



Some examples of using templates for generic programming are:


// Define a generic function template template


T max(T x, T y) return x > y ? x : y; // Instantiate a generic function template with different types of arguments int a = max(10, 20); // T is int double b = max(3.14, 2.71); // T is double std::string c = max("hello", "world"); // T is std::string // Define a generic class template template


class Stack public: void push(const T& x) // ... T pop() // ... private: std::vector data; ; // Instantiate a generic class template with different types of arguments Stack s1; // T is int Stack s2; // T is double Stack s3; // T is std::string // Define a generic variable template template


constexpr T PI = T(3.1415926535897932385); // Use a generic variable template with different types of arguments int d = PI; // T is int double e = PI; // T is double // Define a generic alias template template


using Vector = std::vector; // Use a generic alias template with different types of arguments Vector v1; // T is int Vector v2; // T is double Vector v3; // T is std::string


Use Standard Library Containers and Algorithms




The standard library in C++ provides a rich set of containers and algorithms that can help you store, manipulate, and process data in various ways. Containers are classes that store collections of data, such as std::vector, std::list, std::map, std::set, etc. Algorithms are functions that perform operations on containers or iterators, such as std::sort, std::find, std::count, std::accumulate, etc. Using standard library containers and algorithms can have several advantages:


  • They can save you time and effort from implementing your own data structures and algorithms.



  • They can provide high-quality and well-tested code that is optimized for performance and correctness.



  • They can offer a consistent and uniform interface that is easy to use and understand.



  • They can support generic programming and interoperability by using templates and iterators.



  • They can enhance the readability and expressiveness of your code by using descriptive names and concise syntax.



Some examples of using standard library containers and algorithms are:


// Use standard library containers to store data std::vector v = 1, 2, 3, 4, 5; // a dynamic array of integers "apple", "banana", "cherry"; // a doubly-linked list of strings std::map m = "red", 1, "green", 2, "blue", 3; // a sorted map of key-value pairs std::set s = 3.14, 2.71, 1.41; // a sorted set of unique values // Use standard library algorithms to manipulate data std::sort(v.begin(), v.end()); // sort the vector in ascending order auto it = std::find(l.begin(), l.end(), "banana"); // find an element in the list int c = std::count(m.begin(), m.end(), 2); // count how many elements have a value of 2 double sum = std::accumulate(s.begin(), s.end(), 0.0); // compute the sum of all elements in the set // Use descriptive names and concise syntax to express your code std::vector words = "hello", "world", "foo", "bar"; std::sort(words.begin(), words.end()); // sort the words alphabetically std::reverse(words.begin(), words.end()); // reverse the order of the words for (const auto& word : words) // use a range-based for loop to iterate over the words std::cout


Follow Naming Conventions and Coding Standards




Naming conventions and coding standards are rules and guidelines that help you choose meaningful and consistent names for your variables, functions, classes, etc., and format your code in a clear and uniform way. Following naming conventions and coding standards can have several advantages:


  • They can improve the readability and maintainability of your code, as you can easily understand what each name represents and how each piece of code works.



  • They can reduce the chances of errors and bugs, as you can avoid name conflicts, typos, or misunderstandings.



  • They can facilitate the collaboration and communication among programmers, as you can share a common vocabulary and style.



  • They can enhance the quality and professionalism of your code, as you can follow the best practices and conventions of the C++ community.



There is no universal or official naming convention or coding standard for C++, as different projects or organizations may have different preferences or requirements. However, there are some common and widely used naming conventions and coding standards that you can follow or adapt to your needs. Here are some examples:


Name


Description


Example


Camel case


A style of writing compound words or phrases where each word or abbreviation begins with a capital letter.


SomeClass, someFunction, someVariable


Pascal case


A style of writing compound words or phrases where each word or abbreviation begins with a capital letter and no spaces are used.


SomeClass, SomeFunction, SomeVariable


Snake case


A style of writing compound words or phrases where each word or abbreviation is separated by an underscore and no spaces are used.


some_class, some_function, some_variable


Hungarian notation


A style of writing names that include a prefix that indicates the type or scope of the name.


m_someMember, g_someGlobal, pSomePointer


K&R style


A style of formatting code that places the opening brace at the end of the line that begins a block and the closing brace at the beginning of the line that ends a block.


void foo() if (x > y) // ... else // ...


Allman style


A style of formatting code that places the opening brace on its own line at the same indentation level as the preceding line and the closing brace on its own line at the same indentation level as the opening brace.


void foo() if (x > y) // ... else // ...


Where to Learn More About Exceptional C++ Style?




If you want to learn more about exceptional C++ style, there are many resources and references that you can use. Here are some of them:


  • The books by Herb Sutter, such as Exceptional C++, Exceptional C++ Style, More Exceptional C++, and C++ Coding Standards. These books cover many topics and techniques related to exceptional C++ style, with examples and exercises.



The website of the C++ Core Guidelines, which is an initiative to provide a set of modern and consistent guidelines for using C++. The website contains m


소개

Welcome to the group! You can connect with other members, ge...
bottom of page