Skip to main content
The 2024 Developer Survey results are live! See the results

All Questions

Tagged with
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: ...
ligazetom's user avatar
  • 145
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 << "...
lucottoDA's user avatar
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....
Madhu M N's user avatar
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 *&current, const char8_t ...
Cygon's user avatar
  • 9,560
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 ...
phinz's user avatar
  • 1,451
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 ...
semicolon_missing's user avatar
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 ...
Franz Ferdinand's user avatar
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 = ...
seoubi's user avatar
  • 1
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 ...
U. Windl's user avatar
  • 4,069
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 ...
ConventionalProgrammer's user avatar
-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){ ...
Not A Good Programmer's user avatar
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 ...
francesco's user avatar
  • 7,409
-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 ...
Milosz's user avatar
  • 35
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)&...
DarkLight's user avatar
  • 153
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 ...
Kal's user avatar
  • 475

15 30 50 per page
1
2 3 4 5
29