Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
Results tagged with c++
Search options not deleted
user 78254
Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.
5
votes
Design two classes that are closely related
As so often, it depends.
If your two decoders decode the same format with slight variations (example: different versions of the same format, or maybe XML with or without character entities), then it …
0
votes
When one should use template specialization, if not for metaprogramming?
At some point, some C++ standard libraries contained specializations of the containers for any T*, making them thin wrappers around containers of void*.
This was done to reduce generated code duplica …
12
votes
Accepted
c++ coding practice class vs. "free" functions
When you want to represent distinct concepts, you should create separate types. Yes, this may come with some boilerplate for operator overloading, but having distinct types may offer significant advan …
21
votes
Accepted
Is it considered lazy if I use a linear algebra package for C++?
Richard's comment is most of the answer already. Whenever you're programming for anything but a class exercise, you'll want to reuse as much existing code as possible, with just a few other considerat …
8
votes
Accepted
Can I use C-style casts when calling C functions from C++?
It's easy to prevent the verbosity of calling C functions from cluttering your code: wrap them. In the case of recv, for example, you can simply do this:
template <typename T>
ssize_t recv(int fd, T& …
5
votes
Picking a concrete type based on a configuration parameter
Yes, Factory is the right name for this pattern.
No, C++ does not offer a way to enumerate types. You can perhaps reduce the necessary boilerplate with some clever macros, but I would not suggest it. …
27
votes
Represent individual object as a class?
Classes define behavior.
If your Garfield and Snoopy don't have different behavior from other cats and dogs, they should most definitely not have their own class.
Consider whether you can't just creat …
4
votes
Should a C++ program catch all exceptions and prevent exceptions from bubbling up past main()?
An exception that you catch gives you the opportunity to print a nice error message or even try to recover from the error (possibly by just re-launching the application).
However, in C++ an exception …
3
votes
Return an indicator if the process is succeed or not
Returning something if that something is there, and nothing otherwise, is exactly what std::optional is made for.
If your compiler doesn't yet have this type and you can't upgrade, you can use the Bo …
12
votes
Accepted
Is it bad practice to define constants using class static methods?
Yes, and it's unnecessary too. You can declare constants in headers and define them in source files just like functions:
class AppConstants {
public:
static const int Max;
static const std::strin …
1
vote
Static Polymorphism explained
It gets interesting when Run does other things besides just calling DoIt, and DoIt is just a customization point. So basically,Run is a complicated operation, with small variations. Those variations a …
9
votes
Is there any reason not to use const even if you can?
A class's interface needs to protect its internal state so that its invariants cannot be violated by outside users. (Within reason, of course. C++ makes it impossible to protect against all modificati …
2
votes
Accepted
Run lambda inside a separate thread with a member variable caught by reference
There are two concerns here:
Is there a use-after-free? You need to ensure that the class instance survives long enough for the thread to finish. C++ will generally not help you here, but you could, …
43
votes
Accepted
How to handle failure cases in C++ class constructor?
Yes, though some coding standards may prohibit it.
Yes. The recommended way is to throw an exception. Alternatively, you can store the error information inside the object and provide methods to acces …
2
votes
Accepted
How to "explicitly" declare an implicit interface?
You can use C++20 concepts to add an assertion that your platform mutex really fulfills the requirements.
#include <type_traits>
//#include "platform/Mutex.hpp"
class Mutex {
public:
void lock() …