const-correctness Questions

4

Solved

The description of std::is_void states that: Provides the member constant value that is equal to true, if T is the type void, const void, volatile void, or const volatile void. Then what could be...
Metameric asked 17/6, 2016 at 12:9

6

Solved

I know that where possible you should use the const keyword when passing parameters around by reference or by pointer for readability reasons. Is there any optimizations that the compiler can do if...
Coronation asked 14/12, 2014 at 5:27

2

Solved

I'm an embedded C developer who has recently started messing with C++ code on an embedded device and am unsure about how const-correctness applies when a class accesses volatile data such as memory...
Ballistics asked 22/7, 2013 at 14:23

3

Solved

I have another question related to the safe bool idiom: typedef void (Testable::*bool_type)() const; // const necessary? void this_type_does_not_support_comparisons() const {} // const necessary? ...

2

Solved

Consider the following code: (https://godbolt.org/z/8W699x6q6) int* p; const int*&& r = static_cast<int*&&>(p); Note: const int*&& is an rvalue reference to a pointer...

1

Solved

I'm very confused. I saw that when the code be like void fun(const char **p) { } int main(int argc, char **argv) { fun(argv); getchar(); return 0; } And it shows error: invalid conversion...
Canton asked 27/9, 2023 at 4:57

1

Solved

I have the following code #include <iostream> void foo(const int* const &i_p) { std::cout << &i_p << std::endl; } int main () { int i = 10; int* i_p = &i; std::...

2

I was not expecting this code to compile: #include <iostream> #include <memory> class A { public: inline int get() const { return m_i; } inline void set(const int & i) { ...

1

Solved

I found out by chance that I can have a const std::pair<const int, int>& reference to a std::pair<int,int>: #include <utility> int main() { std::pair<int, int> p{1,2}; ...
Downhaul asked 2/2, 2023 at 15:52

5

Solved

Consider the simple program below, which attempts to iterate through the values of a set using NON-const references to the elements in it: #include <set> #include <iostream> class Int...
Tetzel asked 4/8, 2016 at 13:16

2

Solved

Why can 'operator*' member function of std::unique_ptr be marked const (https://en.cppreference.com/w/cpp/memory/unique_ptr/operator*) while functions like 'front()', 'back()', 'operator[]' etc in ...
Galbraith asked 7/6, 2022 at 12:57

8

Solved

Lets show it in an example where we have a Data class with primary data, some kind of index that points to the primary data, and we also need to expose a const version the index. class Data { publi...
Cent asked 25/4, 2022 at 10:1

3

Solved

So I have some pretty extensive functional code where the main data type is immutable structs/classes. The way I have been declaring immutability is "practically immutable" by making member variabl...
Manicdepressive asked 11/11, 2014 at 5:59

1

Solved

I am getting a Error C2678 binary '*': no operator found which takes a left-hand operand of type 'const _InIt' (or there is no acceptable conversion) it is thrown by this code in MSVC (2022, V17.1...
Bernal asked 18/3, 2022 at 18:34

2

After years of blindly accepting the fact that std::vector<T>::operator[] const returns const_reference, but, in light of how const works for smart pointers, I'm now beginning to wonder why i...
Martini asked 5/1, 2016 at 22:39

6

Solved

can any body tell me how to conver const char* to char*? get_error_from_header(void *ptr, size_t size, size_t nmemb, void *data) { ErrorMsg *error = (ErrorMsg *)data; char* err = strstr((const c...
Necessitarianism asked 7/5, 2009 at 5:41

1

Solved

I thought the only way to declare a const<vector> is: const std::vector<T> v;
Illgotten asked 3/3, 2021 at 15:6

6

Solved

I'm writing software for an embedded system. We are using pointers to access registers of an FPGA device. Some of the registers are read-only, while others are write-only. The write-only regist...
Schreiner asked 17/4, 2013 at 0:12

4

Solved

Recently I have read that it makes sense when returning by value from a function to qualify the return type const for non-builtin types, e.g.: const Result operation() { //..do something.. retur...
Menon asked 9/6, 2011 at 22:15

2

Solved

Please consider the code below, in particular, observing that get_length returns const size_t. #include <stdio.h> const size_t get_length(void) { return 123; } void foo(void) { size_t len...
Greenway asked 19/9, 2020 at 18:23

10

Solved

Consider the following: class A { public: const int c; // must not be modified! A(int _c) : c(_c) { // Nothing here } A(const A& copy) : c(copy.c) { // Nothing here } }; int ma...
Snowcap asked 8/11, 2010 at 13:33

5

Solved

My default behaviour for any objects in local scopes is to make it const. E.g.: auto const cake = bake_cake(arguments); I try to have as little non-functional code as I can as this increases rea...
Orvah asked 24/5, 2020 at 14:46

2

Solved

I have a member variable std::set<T*> m_associates;, i.e., a collection of non-const raw pointers, and simply want to check for existence of another pointer. To maintain const correctness my ...
Melanochroi asked 11/8, 2020 at 13:15

2

GodBolt Consider the following code snippet: using A = void(*)(int); A foo(const void* ptr) { return reinterpret_cast<A>(ptr); } GCC 10 likes this just fine. clang++-10, however, says thi...

4

Solved

Consider the following code: #include <iostream> void f(int const& a, int& b) { b = a+1; } int main() { int c=2; f(c,c); std::cout << c << std::endl; } Function ...

© 2022 - 2025 — McMap. All rights reserved.