CppCoreGuidelines | true guidelines , rules , and best practices | Application Framework library
kandi X-RAY | CppCoreGuidelines Summary
kandi X-RAY | CppCoreGuidelines Summary
"Within C++ is a smaller, simpler, safer language struggling to get out." -- Bjarne Stroustrup. The C++ Core Guidelines are a collaborative effort led by Bjarne Stroustrup, much like the C++ language itself. They are the result of many person-years of discussion and design across a number of organizations. Their design encourages general applicability and broad adoption but they can be freely copied and modified to meet your organization's needs.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Parse md file
- Print the total error counts
- Formats the test suite
- Sets filters
- Return the marker from a line
- Check if the line is a code block
- Write code lines
- Parse command line arguments
- Expand directories
- Print the error categories
- Print the usage message
- Return a set of all supported header extensions
- Processes a file
- Increments the error count
- Determines if the given category should be printed
- Prints the error message
- Determines if a declaration is a declaration
- Checks if the start of the beginning of the file
- Check if a line is a code block
- Print the error counts
- Writes code lines to a file
- Extract the marker from a line
- Collapse strings
- Flag C ++ features
- Check for end of the class definition
- Format the test tests
- Check end of namespace comments
- Removes whitespace from a string
- Returns a set of supported extensions
CppCoreGuidelines Key Features
CppCoreGuidelines Examples and Code Snippets
#include
namespace fs {
class path : public std::filesystem::path
{
};
// The intention is to disallow this function.
bool exists(const fs::path& p) = delete;
} // namespace fs
int main()
{
//fs::path p; // error
std::filesystem::path
Community Discussions
Trending Discussions on CppCoreGuidelines
QUESTION
I'm implementing a generic clone of the Snake game in C++ as an exercise in following the C++ Recommended Guidelines. I'm currently working on the Snake's update() method, which is tiggering a Guideline-violation for using the unchecked subscript operator in a for loop:
Prefer to use gsl::at() instead of unchecked subscript operator (bounds.4).
I want to avoid the redundant range change check of gsl::at() / container::at(), and don't want to suppress the warning either. With that in mind, how can I replace this raw loop with something more Guideline-friendly? (eg. an STL algorithm, iterators, what have you)
...ANSWER
Answered 2022-Jan-04 at 16:36You can std::rotate
the snake so that the old tail is the front element, and then overwrite that with the new segment.
QUESTION
I'm having a rather strange warning being reported by clang-tidy 12.0.1. In the following code:
...ANSWER
Answered 2021-Dec-08 at 13:34To show the actual types involved :
QUESTION
According to CppCoreGuidelines C.60 and C.63, copy assignment operator (e.g Foo& operator=(const Foo& x)
) and move assignement operator (e.g Foo& operator=(const Foo&& x)
) should be declared non-virtual.
Can you explain me the reason of this recommandation ? As suggested by this answer, I imagine that this is to avoid leaks of memory but I don't see exactly how the use of virtual will induce it.
...ANSWER
Answered 2021-Dec-07 at 22:11There is no reason why a copy or move assignment operator should be virtual. Making it virtual possibly incurs costs associated with having virtual functions (e.g. the requirement to allocate a vptr within each object of the class) for no benefit.
Let's say you have a class Base
, which has a virtual copy assignment operator:
QUESTION
If I am using clang tools, what is the recommended way to get clang or some part of the clang toolchain to tell me that e.g. passing an int
to a function that takes a short
might be a bad idea?
Given this very simple program
ANSWER
Answered 2021-Oct-13 at 00:10The -Weverything
option is useful in situations like this. It enables every warning option that clang has, including many that -Wall -Wextra
doesn't include. Many of them are useless or counterproductive, but if there is one that warns on the code you consider problematic, this will let you find it, and tell you which option would enable it specifically. Try it on godbolt.
In this case, using -Weverything
shows us:
QUESTION
The CppCoreGuidlines rule R.33 suggests to
Take a
unique_ptr&
parameter to express that a function reseats the widget.Reason Using
unique_ptr
in this way both documents and enforces the function call’s reseating semantics.Note “reseat” means “making a pointer or a smart pointer refer to a different object.”
I don't understand why we should pass by reference when reseat means "making a pointer or a smart pointer refer to a different object.”
When the function's purpose is to reseat/change the underlying object the pointer is pointing to, aren't we stealing the ownership from the caller this way and therefore should pass the unique_ptr
by value, hence moving it and transferring ownership?
Is there an example that explains why passing a unique_ptr
by reference is recommended?
ANSWER
Answered 2021-Sep-24 at 08:39When the function's purpose is to reseat/change the underlying object the pointer is pointing to, aren't we stealing the ownership from the caller this way
No. Neither when we "reseat" a pointer, nor when we change the pointed object, do we take ownership of the pointer i.e. we aren't transferring the ownership.
Is there an example that explains why passing a unique_ptr by reference is recommended?
Here is an example of a function that "reseats" a unique pointer:
QUESTION
I have variable input
type const std::string&
:
ANSWER
Answered 2021-Sep-08 at 14:34Edit
Apparently type punning with an union is UB so definitely don't do this.
(Keeping the answer for posterity though!)
To strictly answer your question, there's this way:
QUESTION
i am trying to make a program that gave you sum of array elements absolute value .
this is the header file :
...ANSWER
Answered 2021-Sep-05 at 16:16Headers are substituted textually into every compilation unit where they are #include
'd; as a result you have definitions of getAbsSum
in both getAbsSum.cpp
and main.cpp
. This violates the one-definition rule.
The correct way to use a header is to include the declaration in the header and the definition in the cpp file; you have this backwards.
Alternatively, for small functions that don't have a lot of dependencies, you may choose to get rid of getAbsSum.cpp
, mark the definition in the header as inline
, and then anyone including your header will get a definition of the function.
QUESTION
I am looking for the best way to avoid cppcoreguidelines-init-variables
warnings with clang-tidy
.
ANSWER
Answered 2021-Jul-26 at 08:25In general you should initialize variables always because reading from an uninitialized variable is undefined behavior.
When extraction from the stream fails then since C++11 a 0
is assigned. However, this is only the case as long as the stream is not in a fail state. Hence, when reading x
fails then x
will have a value of 0
but the others will have an indetermindate value.
You should always check if extraction succeded:
QUESTION
Move operations should be noexcept
; in the first place for intuitive and reasonable semantics. The second argument is runtime performance. From the Core Guidelines, C.66, "Make move operations noexcept":
A throwing move violates most people’s reasonably assumptions. A non-throwing move will be used more efficiently by standard-library and language facilities.
The canonical example for the performance-part of this guideline is the case when std::vector::push_back
or friends need to grow the buffer. The standard requires a strong exception guarantee here, and this can only move-construct the elements into the new buffer if this is noexcept
- otherwise, it must be copied. I get that, and the difference is visible in benchmarks.
However, apart from this, I have a hard time finding real-world evidence of the positive performance impact of noexcept
move semantics. Skimming through the standard library (libcxx
+ grep
), we see that std::move_if_noexcept
exists, but it's almost not used within the library itself. Similarly, std::is_noexcept_swappable
is merely used for fleshing out conditional noexcept
qualifiers. This doesn't match existing claims, for example this one from "C++ High Performance" by Andrist and Sehr (2nd ed., p. 153):
All algorithms use
std::swap()
andstd::move()
when moving elements around, but only if the move constructor and move assignment are marked noexcept. Therefore, it is important to have these implemented for heavy objects when using algorithms. If they are not available and exception free, the elements will be copied instead.
To break my question into pieces:
- Are there code paths in the standard library similar to the
std::vector::push_back
, that run faster when fed withstd::is_nothrow_move_constructible
types? - Am I correct to conclude that the cited paragraph from the book is not correct?
- Is there an obvious example for when the compiler will reliably generate more runtime-efficient code when a type adheres to the
noexcept
guideline?
I know the third one might be a bit blurry. But if someone could come up with a simple example, this would be great.
...ANSWER
Answered 2021-Mar-03 at 15:44vector push_back, resize, reserve, etc is very important case, as it is expected to be the most used container.
Anyway, take look at std::fuction
as well, I'd expect it to take advantage of noexcept move for small object optimization version.
That is, when functor object is small, and it has noexcept
move constructor, it can be stored in a small buffer in std::function
itself, not on heap. But if the functor doesn't have noexcept
move constructor, it has to be on heap (and don't move when std::function
is moved)
Overall, there ain't too many cases indeed.
QUESTION
I am analyzing a codebase with clang-tidy and see a warning I do not understand. The warning is invoked by the following lines of code:
...ANSWER
Answered 2021-Feb-20 at 11:27Consider the statement...
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install CppCoreGuidelines
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page