All Questions
429
questions
6
votes
1
answer
95
views
Returning const reference from lambda stored as std::function segfaults [duplicate]
Here is the code:
#include <vector>
#include <functional>
class TestClass {
public:
const std::vector<int> &getStuff() const {
return callback();
}
protected:
...
0
votes
1
answer
101
views
Can't understand why copy constructor gets invoked
The following should be a MRE:
#include <iostream>
class Vector {
public:
double x, y, z;
// Default constructor
Vector() : x(0), y(0), z(0) {
std::cout << "...
0
votes
4
answers
97
views
Why is low-level const of a reference not deduced?
When we use auto to deduce type of an object, top-level consts are ignored, for e.g.
const int i = 0; //i is top-level const
auto j = i; //j is an int
However, low-level consts are never ignored, e.g....
0
votes
2
answers
90
views
Can I have a C++ method that accepts both const-ref and non-const-ref pointers?
I have a few helper methods that accept a pointer by reference, advancing it forward if certain conditions are met. Here's an example:
char32_t readCodePoint(const char8_t *¤t, const char8_t ...
1
vote
0
answers
70
views
Why can I add "const" to value parameters but not to reference parameters on member definition? [duplicate]
Look at this code:
struct A{
void f(int);
void g(int&);
};
// this works, defines A::f(int);
void A::f(const int){// no compiler error here despite additional const
};
// why does this ...
0
votes
1
answer
235
views
const reference to a pointer
When I have a const reference to a pointer, how come I am able to change the value of the object the pointer points to, does the const means in this example,
address stored in pointer cannot be ...
3
votes
1
answer
126
views
Can a function with a const-reference as its parameter change the underlying object?
A const reference to a non-const variable can be cast to a non-const reference and then the underlying object can be modified through the reference.
Does it also hold for const-references in function ...
0
votes
1
answer
46
views
I have a question depending on the location of 'const' in C++
int i = 0;
int* lv1 = &(++i); //++i is lvalue
//int* rv1 = &(i++); //i++ is rvalue
const int* &rv1 = &i;
int* const &rv2 = &i;
int* &&rv3 = &i;
const int &rv4 = ...
1
vote
2
answers
87
views
Confusion when defining a list of array refs as constant
In some Perl code I'm doing some low-level coding accessing Perl constants that are lists, like this:
use constant LIST => ()
As Perl constants are actually implemented as functions, I'm trying ...
2
votes
1
answer
139
views
Non-constant object as an argument to a function with constant parameter (C++)
When I create a local variable num inside the main() function and pass this variable into the function dosmth(const int& num) that expects a reference to a constant integer, the program works just ...
-1
votes
2
answers
75
views
Return 'junk value' that is const
float& Matrix_Row::get(const unsigned int &row_indx, const unsigned int &column_indx){
Matrix_Column* temp = head;
while(temp){
if(temp->row_indx == row_indx){
...
0
votes
2
answers
146
views
reference with const access
Using pointers, I can create all combinations of (const/non_const) to (T / const T) for any type T, as discussed in detail this answer.
But using references, how can I define a reference to a variable ...
-1
votes
1
answer
101
views
Returning const string& [duplicate]
While analyzing some code I found something like this
auto foo() const -> std::string const&
It's a bit confusing for me. What does that mean exactly?
To be more precisely I mean about the ...
0
votes
2
answers
147
views
Why can std::function<void(T)> get assigned to std::function<void(T const&)> but not std::function<void(T&)>?
In this code snippet, I don't understand why there is an error below:
#include <functional>
class Foo {};
int main()
{
std::function<void(Foo)> func;
std::function<void(Foo)&...
2
votes
1
answer
100
views
Is const A aref = 3 equivalent to int & const aref = 3 OR int & aref = 3
I am learning C++ using the resources listed here. I came across the following claim which I think is incorrect:
typedef int& A;
const A aref = 3;
because it is equivalent to
int & const ...