C++ resources and a rearrangement of learncpp.com
- cppreference (C/C++ Documentation)
- Compiler Explorer (Running and sharing code)
- Coliru (Somewhat similar)
- C++ Standard Working Draft
| #include "list.h" | |
| List list_instance(size_t type_size) { | |
| return (List){ | |
| .data = malloc(0), | |
| .size = 0, | |
| .type_size = type_size | |
| }; | |
| } |
| #include <stdio.h> | |
| #define py_for(...) _Pragma("GCC diagnostic push")_Pragma("GCC diagnostic ignored \"-Wdangling-else\"") \ | |
| for(struct{int count; bool broke; bool once; bool copy;}py_for = {}; \ | |
| (py_for.count < 2) && !py_for.broke; ++py_for.count)if(!py_for.count)for(__VA_ARGS__) \ | |
| if(py_for.broke){break;}else for(py_for.once = true; (py_for.copy = py_for.broke), \ | |
| (py_for.broke = py_for.once), py_for.once && !py_for.copy; py_for.broke = py_for.once = false) \ | |
| _Pragma("GCC diagnostic pop") | |
| #define py_whl(...) py_for(;__VA_ARGS__;) |
Abstract: This document teaches you from the very basics to advanced features such as std::launder how pointers in C++ work.
It is aimed at developers of any skill level.
However, it links to the C++ standard so that advanced readers can verify the information and investigate further.
Motivation: Most tutorials on pointers are aimed at beginners, and present a simplified model. Some tutorials perpetuate an explanation of pointers that is solely based on their implementation (pointer = memory address). This tutorial aims to provide a comprehensive explanation of pointers that is in line with how they actually work from a language perspective.
| // Compares types of both parameters. Returns true if the types are compatible, otherwise false. | |
| // Parameter y may not be a variably-modified type. | |
| // Designed to be a drop-in replacement for gcc's __builtin_types_compatible_p | |
| #define types_compatible_p(x, y) _Generic(((typeof_unqual(x)*){}), typeof_unqual(y)*:1, default:0) | |
| // Cast the bits of arg to type. Parameter type must be a type and must not be a literal or object with that type. | |
| // Designed to be compatible with g++'s __builtin_bit_cast | |
| #define bit_cast(type, ...) ( \ | |
| union{typeof(__VA_ARGS__) in; typeof(type) out; \ | |
| int enforce_type:_Generic((int(*)(int(type)))0, \ |
I've been writing C++ for half a decade now, and auto has always been a great source of discomfort to me.
Whenever I came back to a past project that makes extensive use of it, I found myself confused,
and first had to look at all the types before I could make sense of it.
Similarly, I've worked as an intern at a company that had a AAA policy for its code base. Whenever something didn't work, and I had to debug some code, half the time was spent just looking up types.
All code you write is ultimately designed to read and write memory. One aspect of this is the memory model of the language; something which is accompanying us as developers, but we rarely have to think about.
How come, for example, the following code does what we expect it to?
int x = 1;
x = 5;inline is redundant for inline definitions, except when modules are used.
In that case, inline would make the function part of the module interface as usual.| main(){char s[10];scanf("%[^\n]",s);printf("(%.*s) %.*s-%.*s",3,s,3,s+3,4,s+6);} |
C++ currently offers only truncating integer division. As a consequence, the remainder operator's sign is same as the sign of the dividend. Alternative rounding modes and remainder sign behaviour are useful.
This proposal adds an <intdiv> header containing free functions for obtaining the quotient and remainder for different rounding modes, such as rounding towards the nearest integer, towards the infinities, etc.