103 questions
6
votes
2
answers
277
views
How do I follow "C.12 - Don’t make data members const or references in a copyable or movable type" while keeping track of some const value?
When I have some const members variables in my class, I would like to know how to respect core guideline C.12 to keep a default copy-assignment operator.
C.12: Don’t make data members const or ...
3
votes
1
answer
260
views
Using CMake, how can we check if ".h" files are "self-contained" (as per guidelines)?
There is the following C++ Core Guideline:
Header files should be self-contained
In large project, how can we automate this check, I would like to avoid having to add empty .cpp files for every &...
2
votes
1
answer
166
views
Should we still always std::forward a universal reference argument even if unnecessary?
Consider the following code:
#include <string>
auto f1(auto&& v) {
return v;
}
auto f2(auto&& v) {
return std::forward<decltype(v)>(v);
}
int main() {
return ...
2
votes
1
answer
173
views
Is Cpp Core Guidelines Rule F19 incomplete?
Cpp Core Guideline F19 tells us
Flag a function that takes a TP&& parameter (where TP is a template type parameter name) and does anything with it other than std::forwarding it exactly once ...
-3
votes
1
answer
363
views
Why does C++ Core Guidelines prefer simplest “singleton” as a function but not as a static member function?
C++ Core Guidelines, I.3: Avoid singletons:
Exception You can use the simplest “singleton” (so simple that it is often not considered a singleton) to get initialization on first use, if any:
X& ...
4
votes
1
answer
379
views
Cpp Core Guidelines: "const char *" to "const uint8_t *" without reinterpret_cast and C-style cast?
For code like this:
#include <cstdint>
extern const char *f();
extern void g(const uint8_t *);
int main()
{
const char *p = f();
g(reinterpret_cast<const uint8_t *>(p));
}
clang-...
1
vote
1
answer
1k
views
Why do the C++ Core Guidelines not recommend to use std::optional over pointers when approriate? [closed]
In the C++ Core Guidelines std::optional is only referred once:
If you need the notion of an optional value, use a pointer, std::optional, or a special value used to denote “no value.”
Other than ...
1
vote
1
answer
344
views
How can I decay const char that is passed as reference to a function with variadic parameters?
I have a function like this:
void column(const std::string &value) { ... }
void column(float value) { ... }
template <class... TColumns> void row(const TColumns &...columns) {
ImGui::...
2
votes
1
answer
1k
views
How to prevent ODR violations in this case?
disclaimer: this question is about prevention of unintended naming collisions, and make sure the following code fail to compile/link.
[edit] actually I'd be happy with something preventing this to ...
1
vote
1
answer
621
views
How do I define __cpp_exceptions for gsl:narrow to compile?
I am getting confused again :(
I have looked at this discussion:
detect at compile time whether exceptions are disabled
I am new to trying to use GSL. I have copied the GSL folder to my PC and added a ...
0
votes
0
answers
37
views
About using directives in namespace in header files [duplicate]
(This is kind of related to a previous question.)
Core guideline SF.7 gives a good motivation for avoiding to put using namespace directives at global scope in a header file.
However, even writing ...
4
votes
2
answers
382
views
Are you meant to recover from contract violations?
With the guideline support library and utilities like gsl_Expects C++ implements contracts for the time being (there are plans to bake this stuff into the language in the future). Using this feature ...
1
vote
0
answers
315
views
C++ Core Guidelines: Lifetime - why does the following code trigger a warning?
When compiling the following code in Visual Studio 2019 while using the "C++ Core Check Lifetime Rules", I get
warning C26486: Don't pass a pointer that may be invalid to a function. ...
-1
votes
1
answer
242
views
Include directives order in source file and in header file [closed]
Which is the recommended order in which the #include directives are supposed to be listed? I could not find any answer in the C++ Core Guidelines
For example, should they be ordered like this:
#...
2
votes
0
answers
2k
views
Clang-Tidy Narrowing Conversion from uint8_t to float
I'm getting a clang-tidy warning that reads narrowing conversion from 'int' to 'float' when I convert from a uint8_t to a float, which to my understanding is not a narrowing conversion since float can ...
16
votes
3
answers
6k
views
C++ Core Guidelines for static member variables
I have a private static vector in my class that keeps a pointer to all objects created from it. It's necessary as each object needs access to information from all the other objects to perform some ...
6
votes
4
answers
2k
views
How to properly use "C++ Core Guidelines: C.146: Use dynamic_cast where class hierarchy navigation is unavoidable"
Motivation
The C++ Core Guidelines recommends using dynamic_cast when "class hierarchy navigation is unavoidable." This triggers clang-tidy to throw the following error: Do not use ...
5
votes
2
answers
235
views
In C++ Core Guidelines Per.4, why is the bad example intended to be faster?
I am reading this recently, which states:
Don’t assume that complicated code is necessarily faster than simple code.
The code is copied as following:
Example, good
// clear expression of intent, ...
10
votes
1
answer
11k
views
C++ Warning: Assigning newly created gsl::owner<> to non-owner
When I use the following code, I get a warning (From applying cppcoreguideline).
Code:
SampleClass *object = nullptr;
object = new SampleClass();
Warning:
warning: assigning newly created 'gsl::...
0
votes
2
answers
443
views
Assert is seen as C style cast in Visual Studio
Here is the error and a glimpse of the code One of my courses demands me to use Warning Level 4 and to treat warnings as errors in Visual Studio. Beside that, we also need to activate Cpp Core ...
0
votes
1
answer
2k
views
How to "reset" gsl::owner<T>?
When I create an object and append it to a list
auto o = new object;
m_objects.push_back(o);
I get several hints from the compiler that I should clean up my code along the C++ Core Check guidelines, ...
1
vote
1
answer
215
views
How to handle static analysis warning from Core Guidelines checker about gsl::at?
I activated static analysis for my project in Visual Studio. The Core Guidelines checker says i should use gsl::at for subscription. But my code is save. What's the cleanest way to get rid of this ...
3
votes
1
answer
438
views
When using lambdas for complex initialization of variables, how can I handle outside of the lambda exceptions that are thrown from inside?
I am using lambdas to initialize some const variables as described in the core c++ guidelines here. In short, the idiom looks like this
const auto a = [&]() {
MyType a;
// complex ...
4
votes
1
answer
2k
views
How can I use unions without clang-tidy warnings?
Clang-tidy's cppcoreguidelines-pro-type-union-access rule is essentially a complete ban on unions, it flags all access to union members.
My library has an extern "C" interface with a structure which ...
13
votes
2
answers
1k
views
How can a compiler generated default constructor be more efficient than a self-written one that does nothing but initialize members?
Triggered by this answer I was reading in the core guidelines:
C.45: Don’t define a default constructor that only initializes data
members; use in-class member initializers instead
The reasoning ...