1,472 questions
6
votes
1
answer
193
views
Is binding-level mutability necessary for a borrow checker, or could Rust have supported type-level const like C++?
I am new to Rust and one thing that stands out to me is that in Rust, constness/mutability is not bound to the type, but to a particular variable. So, it seems to me like it would be impossible in ...
58
votes
1
answer
5k
views
Why do bit operators have such low precedence in C and C++?
In C and C++, the expression some_num & 0xABCD == 5 will effectively evaluate as some_num & (0xABCD == 5). This is unlike all of the standard arithmetic operators, as they have higher ...
0
votes
1
answer
156
views
Is there such a thing as a non-copiable type in Haskell?
In short, I'm trying to understand why copying, which is such a fundamental thing in C++ (fundamental in the sense that you, as the programmer, have quite a lot of power in writing code to permit or ...
5
votes
1
answer
303
views
Is there really not cross product in std::linalg and if yes why?
I know for sure how to calculate a cross product myself, but usually I tree to use the standard library where possible.
I am aware that std::linalg is still in development so this might already be the ...
0
votes
2
answers
188
views
Why is the virtual keyword applied to methods instead of classes
When the virtual keyword is applied to a method the whole class becomes abstract.
Shouldn't the virtual or abstract keyword be applied to the class? It is a more intuitive design because it reflects ...
4
votes
1
answer
169
views
Why doesn't std::array's operator[] retain the value category of the array?
I'd have expected that if an std::array is an rvalue, then calling operator[] returns an rvalue as well. But it doesn't seem to be the case (cppreference), there are no value-category overloads.
For ...
1
vote
1
answer
138
views
How to implement ordered fan-in (proper message passing for my language)?
I'm the creator of https://github.com/nevalang/neva
It's a dataflow programming where you have nodes that do message passing through ports. I use go channels to implement that. However, I faced an ...
4
votes
1
answer
163
views
Why does the standard require only input iterators for std::distance, rather than forward iterators?
I was perplexed to see that the template parameter requirement for std::distance is a LegacyInputIterator rather than a LegacyForwardIterator.
Since input-only iterators don't have the multi-pass ...
3
votes
0
answers
47
views
Why is the overload for both the == and <=> operator required to overload all comparison operators? [duplicate]
I was going through this guide on operator overloading in C++.
If I try to implement all the comparison operators using the <=> operator:
friend auto operator<=>(const Some_class& lhs, ...
6
votes
1
answer
941
views
Why `std::string_view` is not modifiable?
I start experiment with std::string_view.
It has a very ugly feature. You cannot say:
std::string_view<const char> and std::string_view<char> like the awesome std::span.
So, you cannot ...
3
votes
1
answer
95
views
Why is initialization of inline/non-inline variables indeterminately sequenced?
int x = f();
inline int y = x;
It is unspecified whether y is zero or the value of x.
Note that x has ordered initialization and y has partially-ordered initialization (see [basic.start.dynamic] p1).
...
0
votes
2
answers
147
views
Why is it illegal to call offsetof() on pointer member?
From there I know that it is illegal in C to call offsetof on a pointer, but why is it undefined behaviour? Standard implementation
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)...
0
votes
1
answer
71
views
Can any usage of the assignment operator technically be replaced with the walrus operator surrounded by parentheses?
Say we have this assignment statement:
a = 5
Even though it'd obviously be ugly to do this for no reason, we could technically accomplish the same thing with:
(a := 5)
This still assigns 5 to a, and ...
0
votes
0
answers
45
views
From Abstract Syntax Tree to machine code. Functions as first-class objects compilation?
I have created a front-end for my language. I have also been able to transpile to C so that it can be compiled. In this language, functions are treated as first-class objects.
Is it possible to ...
1
vote
2
answers
167
views
Why are pre-allocated stacks expensive, given 64-bit virtual memory?
As https://without.boats/blog/why-async-rust/ puts it,
OS threads have a large pre-allocated stack, which increases per-thread memory overhead.
And the article goes on to argue that much of the ...
2
votes
1
answer
96
views
Detecting shared structure in tree made of cons cells
I'm writing a programming language (details irrelevant) which uses Lisp-like cons cells to store its data (this makes implementing the garbage collector easy). I'll spare you the details of everything ...
24
votes
2
answers
3k
views
Is it possible to make zero-allocation coroutine runtime in C++?
In Rust, asynchronous functions do not require allocating on the heap. Function async returns a compiler-generated structure on which you call a compiler generated poll method. The design of async ...
-1
votes
2
answers
109
views
Rationale behind C++ member variables declaration [closed]
I am still pretty new to c++ and would like to understand the thought process behind creating member variables the way it is. For example, if I go through a header file, I see some of the member ...
0
votes
1
answer
115
views
NAN Box Negative Int
I have been followings this article which explains nan boxing https://piotrduperas.com/posts/nan-boxing and tried to implement it in my own "language".
typedef union {
uint64_t as_uint;
...
1
vote
1
answer
70
views
Defining an infinite family of classes "MatrixMxN" in C#
Suppose I was writing a linear algebra library in C#, and I wanted to implement matrix multiplication. Of course, two matrices A and B can only be multiplied to form AB if A has as many columns as B ...
0
votes
0
answers
72
views
Why is the std::iterator_traits::iterator_category for pointers not std::contiguous_iterator_tag?
iterator_traits is specialized for pointers as
namespace std {
template<class T>
requires is_object_v<T>
struct iterator_traits<T*> {
using iterator_concept = ...
22
votes
3
answers
3k
views
Why does ranges::for_each return the function?
The legacy std::for_each returns function as the standard only requires Function to meet Cpp17MoveConstructible according to [alg.foreach]:
template<class InputIterator, class Function>
...
1
vote
1
answer
85
views
Why is std::make_shared<T> not a static function of std::shared_ptr<T>, i.e. std::shared_ptr<T>::make?
This is a question about understanding a design decision, not a complaint about bugs or flaws.
In the C++ standard library, the function to create a shared pointer and its object is a straight ...
0
votes
1
answer
85
views
Discriminating between a Lambda Parameter List and a Grouping Expression in a Recursive Descent Parser
I am writing a small scripting language for a project I am working on. I wrote a simple recursive-descent parser for it (similar to the one in the Crafting Interpreters). I wanted to add support for ...
0
votes
0
answers
227
views
LLVM pointers dereference
In my main function, I have allocated an i8 pointer:
%a = alloca i8*, align 8
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @1, i32 0, i32 0), i8** %a, align 8
Is it possible to:
assign a ...