Questions tagged [c++-faq]
Provides a collaborative, community-edited C++ FAQ
c++-faq
170
questions
2
votes
0
answers
145
views
C++17 Copy elision object lifetime
From cppreference:
URVO is mandatory and no longer considered a form of copy elision.
When copy elision occurs, the implementation treats the source and
target of the omitted copy/move(since C++11) ...
2
votes
1
answer
179
views
Prvalue semantics object lifetime
I'm confused about the lifecycle of objects involved in prvalue semantics. For example:
struct A {
int *num;
A(): num{new int(1)} {}
A(A &&other) { num = std::move(other.num); std::cout &...
-2
votes
1
answer
465
views
What is the precedence and associativity of operators?
What is the precedence and associativity of operators in C++?
Who defines operator precedence and associativity, and how does it relate to order of evaluation? explains how those properties emerge ...
4
votes
1
answer
1k
views
Global variables - When to use static, inline, extern, const, and constexpr
There are plenty of questions and answers relating to C++ global variables, such as:
static and extern global variables in C and C++
Global variables and constexpr (inline or not?)
Is there any sense ...
14
votes
1
answer
4k
views
What is an mdspan, and what is it used for?
Over the past year or so I've noticed a few C++-related answers on StackOverflow refer to mdspan's - but I've never actually seen these in C++ code. I tried looking for them in my C++ compiler's ...
36
votes
1
answer
26k
views
What is std::expected in C++?
In one of the most respected stackoverflow answer I found an example of std::expected template class usages:
What are coroutines in C++20?
At the same time I cannot find any mentioning of this class ...
9
votes
2
answers
3k
views
Is there a "simple" way to have ld output demangled funtion names?
I find very frustrating to have to fix C++ errors happening at linkage time (especially undefined reference errors) due to the fact that all the function names are mangled. An example of mangled name:
...
4
votes
1
answer
321
views
Difference between preprocessor macros and std::source_location
There are some predefined preprocessor macros (specified in C and C++ standard) like __line__ and __file__ which are replaced by line number and file name during preprocessing respectively. In C++20, ...
17
votes
1
answer
3k
views
How to create stream which handles both input and output in C++?
I'm trying to make a class that will be both input and output stream (like std::cout and std::cin ). I tried to overload operator << and >>, but then, I understood that writing such code ...
6
votes
1
answer
353
views
Can I add a deduction guide to `std` namespace?
Suppose I want to make a new deduction guide making the following possible ?
std::string str;
std::basic_string_view sv = str;
Would that be an Ok customization ?
355
votes
21
answers
43k
views
Why should I always enable compiler warnings?
I often hear that when compiling C and C++ programs I should "always enable compiler warnings". Why is this necessary? How do I do that?
Sometimes I also hear that I should "treat warnings as errors"...
10
votes
1
answer
412
views
Using placement new in generic programming
When using placement new in generic code to construct an object at a specified address, the usage pattern is a bit different from usual code. For example, consider this implementation of ...
68
votes
5
answers
151k
views
How to write C++ getters and setters
If I need to write a setter and/or getter for I write it like this:
struct X { /*...*/};
class Foo
{
private:
X x_;
public:
void set_x(X value)
{
x_ = value;
}
X get_x()
...
450
votes
4
answers
190k
views
What is a "span" and when should I use one?
Recently I've gotten suggestions to use span<T>'s in my code, or have seen some answers here on the site which use span's - supposedly some kind of container. But - I can't find anything like ...
110
votes
3
answers
13k
views
Why doesn't a simple "Hello World"-style program compile with Turbo C++?
I have started learning C++ for my programming class. I have downloaded this "Hello World" program:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
...