678 questions
Score of 5
2 answers
217 views
For restrict pointer `p`, the definition of Based-On Implies that given `int* n = p`, `&*n` is not based on `p`
The C99 standard draft n1256 says the following about restrict qualified pointers in 6.7.3.1p3, 6.7.3.1p4
3 In what follows, a pointer expression E is said to be based on object P if (at some ...
Score of 0
0 answers
102 views
How can I restrict a user to passing in a const lvalue reference [duplicate]
I have a class which stores a const pointer to some external data. To get the pointer, the user passes in an object by reference. The object should be permitted to be const. As well as the class I ...
Score of -4
4 answers
147 views
Why does this assignment fail but not cause an error?
In this python I expect either for x to be assigned to 1, OR for an error/exception to be raised. Instead, nothing happens. The statement is not an error but it also has no effect.
x = 0
[x][0] = 1
...
Score of 3
3 answers
211 views
Is a function call returning a pointer a prvalue?
This question arouse from the question "why is const in function return type, which is a const pointer, ignored?" (const pointer, not pointer to const)
int* const Foo();
is identical to
int*...
Score of 5
2 answers
329 views
Can an rvalue of a specific class type be automatically cast to an lvalue of itself?
In C++, a function with signature void f(A& a) (non-template) only binds to a lvalue.
Is it possible for an rvalue to automatically cast itself to an lvalue?
This is what I tried:
#include <...
Score of 13
5 answers
2037 views
How Does the Left-Hand Side (LHS) of an Assignment Evaluate in C?
I’m working on understanding pointers in C by breaking down the language into three key areas: syntax, semantics, and idioms.
To grasp pointers, I’ve been focusing on fundamental concepts:
what’s an ...
Score of 4
1 answer
88 views
Store value category of pointed-to object (for later deref)
Ok this might be a bit difficult to explain:
I want to call a function hello that has an overload for either an rvalue or an lvalue ref of type Json. This Json object is converted from a JsonKey using ...
Score of 4
1 answer
119 views
Cannot return a named rvalue reference in a function with return type of lvalue reference?
In C++, a named rvalue reference is lvalue.
Why the below code yields "error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'"?
int & unmove(int &...
Score of 1
2 answers
177 views
Are compound literals always lvalue?
I was reading C Notes for Professionals, in which, it claims that Compound Literals can only be lvalues. But LLMs have mixed answers upon that stating it could depend upon the context and could be ...
Score of 2
1 answer
112 views
Why does passing rvalue to lvalue reference work in this case?
I was doing Leetcode when I stumbled upon this interesting technique. Here it is in short:
#include <iostream>
#include <vector>
void print(std::vector<int>& vec)
{
for (int ...
Score of 27
3 answers
5466 views
Is `(expression, lvalue) = rvalue` a valid assignment in C or C++? Why do some compilers accept/reject it?
Some time ago I stumbled upon the idea of how a C construct, such as (expr0, expr1, expr2), evaluates (see "What does the comma operator , do?" for more context).
I've started experimenting ...
Score of 1
1 answer
278 views
C++ Does Ranged-Based For Loop Use RValue Reference? [duplicate]
Hi I have a quick question - over here it says ranged-based for loops of the form
for ( init-statement (optional) range-declaration : range-expression )
are equivalent to the code:
{
auto &&...
Score of 1
2 answers
92 views
Why does this function return a pointer instead of pointer's value in C language?
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int *array;
int size;
}Array;
Array array_create(int init_size);
int* array_at(Array *,int index);
void ...
Score of 2
1 answer
81 views
Can a reference to an lvalue subroutine be used as an lvalue?
I'm having trouble using a Perl subroutine reference as an lvalue. Am I doing something wrong here, or is it not possible to do this? I'm using ActiveState Perl 5.20 on Windows.
package Warning {
...
Score of -2
2 answers
1266 views
Why am I getting an error, "non-const lvalue reference to type 'std::basic_string<char>' cannot bind to a value of unrelated type 'const char [4]'" [duplicate]
I had a queue and originally worked with strings and now I added templates to it so that if I decide to add int doubles, etc, it would still work. When using strings to add to my queue, I get an error ...