Skip to main content
Filter by
Sorted by
Tagged with
16 votes
3 answers
919 views

I have a function template f, defining in its body a local class A with another nested class B. Both classes are not templates. Must I name the inner class as typename A::B or shorter variant A::B is ...
Fedor's user avatar
  • 24.7k
11 votes
4 answers
1k views

The program test tag dispatching pattern, where the function process_data(tag, a, b) can accept 2 tags int_tag, float_tag. There are 3 case: int_tag and a, b are int -> print a + b float_tag and a,...
Huy Le's user avatar
  • 1,989
12 votes
8 answers
1k views

I want to use circular queue with different types and lengths in a project which mainly consists of C code. I'm thinking about implementing the circular queue as a C++ template struct. How to expose ...
Jackoo's user avatar
  • 533
Advice
1 vote
21 replies
329 views

From the man pages, I found that size_t has the range of 0 to SIZE_MAX, and ssize_t has the range of -1 to SSIZE_MAX. So, after printing those values on a 64bit system, I have the following results: ...
sat0sh1c's user avatar
Advice
2 votes
4 replies
249 views

Any C++ class can be first forward declared, and defined only later in the program. Are function-local classes an exception from this rule? Please consider a simplified program: auto f() { struct ...
Fedor's user avatar
  • 24.7k
9 votes
2 answers
698 views

C++, using <chrono>, I would like to get a value formatted as <seconds>.<milliseconds>. I am using system_clock and get the current seconds as: auto secs = duration_cast<seconds&...
Ender's user avatar
  • 1,922
6 votes
6 answers
245 views

Let's say I have the following code: #include <array> #include <string> #include <variant> using namespace std::literals; using known_types_t = std::variant<int, double, char>...
Miguel Horta's user avatar
18 votes
1 answer
2k views

I am trying to track if a C++20 coroutine has ever suspended, so that unhandled_exception knows whether it can simply re-throw; the exception back to the caller of the initial coroutine function, or ...
LB--'s user avatar
  • 3,228
2 votes
3 answers
339 views

I need to make a linked list implementation without using STL. push_front function has two overloads: one with const T&, the other with T&&, but the implementation is the same. The only ...
Paul-dev's user avatar
Advice
0 votes
11 replies
205 views

The following code is accepted by GCC and the resulting binary prints the expected results. But is it standard conform and would always work on different systems using different compilers? Minimal ...
Martin Fehrs's user avatar
  • 1,175
Advice
5 votes
2 replies
5k views

Can I express a function that consumes an object? Meaning that its destructor is not run on the moved-from object? Like the proposed library function trivially_locate_at itself? template <class T&...
user2394284's user avatar
  • 6,164
Advice
1 vote
14 replies
250 views

I need help regarding a code I want to write in c++. I want to develop a program that receives and visualizes CAN messages on a GUI. The messages in question are about 100 distinct ID's, so we're not ...
Simone Sesana's user avatar
9 votes
1 answer
915 views

With C++26 reflection, we can simply iterate all data members of an object: #include <iostream> #include <meta> struct Foo { int x; int y; }; void PrintFoo(const Foo& foo) { ...
Timothy Liu's user avatar
3 votes
2 answers
156 views

In the below code I'm trying to print an unsigned char variable using std::cout and std::println. Using std::cout prints the character while std::println prints the ascii value. Can somebody explain ...
Harry's user avatar
  • 4,144
7 votes
2 answers
731 views

Assuming two overloads: void X::f(int, float); void X::f(long, double); is it possible, in C++17 or higher, to infer int/long (or whatever it is) from float/double for the second argument? To be used ...
Gleb Belov's user avatar
0 votes
3 answers
311 views

I am new to C++, I came from Python where there are constructors but no destructors. What is the point of them if the memory is automatically freed when the object goes out of scope ?
Amogus124's user avatar
Best practices
0 votes
8 replies
196 views

My confusion is about the return statement inside readFileContents: The function returns std::optional<std::string>, but the local variable is a std::string. NRVO doesn’t apply here because ...
sam's user avatar
  • 911
Best practices
3 votes
9 replies
220 views

In a framework that makes use of the std::int* types (such as std::int16_t) as well as the std::int_fast* types (such as std::int_fast16_t) there could be general rules where one could be better than ...
asimes's user avatar
  • 6,202
Advice
0 votes
6 replies
202 views

I need to layout a structure in two different ways depending on the endianness of the target platform. Currently I'm using an additional pre-compile phase to run a program to test the endianness, and ...
Alnitak's user avatar
  • 341k
9 votes
1 answer
617 views

I noticed a discrepancy between C++23 and C++26 code when using googlemock: enum A { A1, A2 }; enum B { B1, B2 }; TEST(...) { ASSERT_EQ(A1, B1); // compiles fine in C++23 and C++26 ASSERT_THAT(...
PiotrNycz's user avatar
  • 25.1k
8 votes
1 answer
830 views

Does the code below contain undefined behavior (UB)? struct A { int x; char y; }; int main() { std::vector<uint8_t> v(sizeof(A), 0); A* p = reinterpret_cast<A*>(v.data());...
Dmitriano's user avatar
  • 2,474
Advice
3 votes
7 replies
227 views

Off the top of my head, I only remember LLVM's standard library do this, but I remember seeing other libraries also do this. I'm not entirely sure what is the advantage here. I feel like namespace std ...
Somedude's user avatar
3 votes
2 answers
277 views

I see many places in public repositories, where the first and last iterators of std::vector/std::string/std::string_view are converted into pointers using the combination of &* operators. In ...
Fedor's user avatar
  • 24.7k
Advice
0 votes
13 replies
210 views

I'm wondering how the truncate function works. C++ is just an example i'm using because it's a language I know. I've searched online and I can't seem to find how it works, simply how to use it and ...
nugget's user avatar
  • 71
11 votes
1 answer
654 views

Consider the following simplified code: template <class T> class Foo { T t; const T* t_ptr; public: constexpr Foo(T t): t(t), t_ptr(&this->t) {} constexpr Foo(const Foo&...
eyelash's user avatar
  • 4,126
Advice
5 votes
6 replies
181 views

I would like to create a template function taking an std::array<int, N> as a nontype template argument for any N (you can do that since C++20). Explicitly I can do it like that: #include <...
CygnusX1's user avatar
  • 22.1k
9 votes
1 answer
487 views

Consider this code: #include <cstddef> #include <iostream> const double cash = 1000; int main() { size_t my_size_t_value = 2; double my_double_value = -my_size_t_value*...
Joe Chakra's user avatar
2 votes
3 answers
239 views

I implemented std::hash<std::string> in the usual way, by specializing the std::hash template. But then I realized that these should already be provided by the <string> header (https://www....
luczzz's user avatar
  • 446
10 votes
3 answers
1k views

I've always assumed that "creating an object" is the same thing as "starting its lifetime" (and not the same thing as allocating storage for it). But I've recently been told that &...
HolyBlackCat's user avatar
Advice
0 votes
7 replies
334 views

I'm looking for a way to store type "dynamicly" in a using (or concret implementation) that would be modifable and accessible at compile-time. I would like something like: struct ...
DipStax's user avatar
  • 572
Best practices
0 votes
31 replies
200 views

Does the code below violates Liskov Substitution Principle (LSP)? class Object { public: virtual ~Object() = default; }; class A : public Object { public: void print() { std::...
Dmitriano's user avatar
  • 2,474
5 votes
4 answers
349 views

If I have a long string: std::string data("This is a string long enough so that it is not a short string"); And then I have a view onto that string: std::string_view dataView(std::begin(...
Loki Astari's user avatar
4 votes
2 answers
161 views

I am implementing type-erased iterators for code that uses std::views and have found a problem while trying to compare values that wrap sentinels. Basically, it seems that for some composition of ...
Krzysiek Karbowiak's user avatar
3 votes
2 answers
185 views

I've a C++ concept where I need to check that the class has a particular public attribute. My problem is that the concept works if I use it directly, but fails if I use it in std::visit. This is the ...
Jepessen's user avatar
  • 12.7k
Advice
0 votes
9 replies
184 views

Is it possible to map the enum values to underlaying data types to use them with std::variant? I want to do something like the example below, but want the std::variant to understand it is an int or ...
sav's user avatar
  • 76
Advice
1 vote
7 replies
179 views

In the question Why does Microsoft's implementation of std::string require 40 bytes on the stack? the observation is made, that a std::string requires 8 additional bytes in Debug mode. After ...
Brandlingo's user avatar
  • 3,242
5 votes
2 answers
293 views

I have this code in C++: #include <iostream> #include <iomanip> #include <limits> #include <boost/multiprecision/mpfr.hpp> using namespace boost::multiprecision; template<...
Stasiu222's user avatar
Best practices
0 votes
7 replies
173 views

I would like use bit-fields to access low level memory. I'm aware of the non-portability of bitfields but they do appear to be consistently implemented on my platform (cortex-m4) on both clang and gcc....
Lee's user avatar
  • 51
6 votes
2 answers
185 views

I'm using the Stringzilla string library, which is supposed to be close to how std::string operates. It has a flag called SZ_USE_MISALIGNED_LOADS. By default it sets it to on, and it looks like this: /...
Zebrafish's user avatar
  • 16.3k
4 votes
2 answers
125 views

I need to get a unique ID which I could use as a template argument (which means it must be statically determined at compile-time), for each member function of my class. My first idea was to use the ...
mardy's user avatar
  • 218
-2 votes
2 answers
250 views

Consider this example: extern void black_box_foo(); extern void black_box_bar(); int main(){ black_box_foo(); // #1 black_box_bar(); // #2 } #1 and #2 are functions whose definitions are ...
xmh0511's user avatar
  • 7,578
Advice
0 votes
6 replies
193 views

I have wanted to make a GUI library so that I can use it in my own video game that I am making and maybe in the future have it be used for other things like software. I wanted to ask where to start ...
ShizamDaGeek's user avatar
4 votes
1 answer
263 views

I'm testing int matrix multiplication, but I found that it's extremely slow everywhere (python numpy using BLAS backend is also just as slow). Int matmul being slower than float matmul is ...
Huy Le's user avatar
  • 1,989
5 votes
1 answer
168 views

The same code, one generated by macros and the other handwritten, produces different results.I'm lost and don't know how to fix it. my environment is follow: vs2019, msvc2019_64, c++14 if using ...
user31892351's user avatar
4 votes
2 answers
141 views

AWS S3 uses CRC64/NVME for its default checksum algorithm. I need to send a trailing CRC64/NVME checksum. The kicker is that I am middleware. There are multiple threads that are sending bytes. I ...
Justin James's user avatar
4 votes
2 answers
271 views

I am probably familiar with strict aliasing rule and implicit lifetime creation in a certain degree, but I still do not understand well enough how do these terms relate to std::launder. The following ...
Dmitriano's user avatar
  • 2,474
0 votes
4 answers
157 views

I was attempting the binary tree inversion question on Leetcode https://leetcode.com/problems/invert-binary-tree/ I came up with this solution TreeNode* invertTree(TreeNode* root) { if (root ==...
Raghav Wadhwa's user avatar
9 votes
1 answer
257 views

Suppose the following foo function has a function parameter and a template parameter, both with the same default value: template<int Line=std::source_location::current().line()> auto foo (int ...
abcdefg's user avatar
  • 4,637
3 votes
1 answer
168 views

In the below code, I return a std::vector<A> from the get() function. If I make the std::vector<A> be a const type, the compilation fails, otherwise it passes. Can somebody explain this ...
Harry's user avatar
  • 4,144
1 vote
2 answers
149 views

Take the below piece of code, that simply Trims a string, removing whitespace characters from either end: const std::string TrimString(const std::string& s) { const auto iter = std::find_if(s....
The Welder's user avatar
  • 1,099

1
2 3 4 5
7