constants

Intermediate pointers in cast must be “const qualified” - why?

五迷三道 提交于 2020-07-08 04:50:11
问题 In the following code... #include <stdlib.h> #include <stdint.h> extern void get_buffer_from_HW_driver(volatile uint32_t **p); void getBuffer(volatile uint32_t **pp) { // Write an address into pp, that is obtained from a driver // The underlying HW will be DMA-ing into this address, // so the data pointed-to by the pointer returned by this // call are volatile. get_buffer_from_HW_driver(pp); } void work() { uint32_t *p = NULL; getBuffer((volatile uint32_t **)&p); } ...the compiler rightfully

How do global consts that are not copy or clone work in Rust?

二次信任 提交于 2020-07-08 03:55:10
问题 Say I have the following snippet (playground) struct A { pub val: u32 } const GLOBAL_A: A = A {val: 2}; fn main() { let some_a: A = GLOBAL_A; let other_a: A = GLOBAL_A; println!("double val = {}", some_a.val + other_a.val); } Since A is neither Clone nor Copy , I would assume the value of GLOBAL_A would be moved. That does not make much sense for a const and as shown cannot be the case anyways since it can be "moved" twice. What are the rules that allow the above snippet to work considering A

How Can I Access A Constant Variable With Only A String

拥有回忆 提交于 2020-07-08 03:32:34
问题 I have the following issue: const value1 = "some value"; var value2 = "some value"; console.log(window["value1"]); // undefined console.log(window["value2"]); // some value I know that const is block scoped and that is the reason I cannot access it through the window object. My question is, is there any way to access a const variable using only a string identifier. If all I have access to is "value1", how do I get a handle on the actual value1? Is it even possible? 回答1: It appears that, block

Header file containing 'const' included in multiple source files

半世苍凉 提交于 2020-07-07 09:46:11
问题 Why does not a header file containing the definition for a const and included by multiple source files give a compilation error for multiple definition ? const_in_header_file.h const int num = 5; //int x; //Error. Multiple defintion if included from multiple source files. const_in_header_file_func.cpp #include <iostream> #include "const_in_header_file.h" using namespace std; void func(void) { cout << "num in func() = " << num << endl; } const_in_header_file_main.cpp #include <iostream>

Header file containing 'const' included in multiple source files

徘徊边缘 提交于 2020-07-07 09:45:13
问题 Why does not a header file containing the definition for a const and included by multiple source files give a compilation error for multiple definition ? const_in_header_file.h const int num = 5; //int x; //Error. Multiple defintion if included from multiple source files. const_in_header_file_func.cpp #include <iostream> #include "const_in_header_file.h" using namespace std; void func(void) { cout << "num in func() = " << num << endl; } const_in_header_file_main.cpp #include <iostream>

Is it UB to call a non-const method on const instance when the method does not modify members? [duplicate]

拜拜、爱过 提交于 2020-07-05 07:58:05
问题 This question already has answers here : Is const-casting away const-ness of references to actual const objects permitted if they are never modified through them? (2 answers) Const casting empty base class (1 answer) Closed 10 days ago . Code speaks more than thousand words, so... This is undefined behaviour for mutating a const int : struct foo { int x; void modify() { x = 3; } }; void test_foo(const foo& f) { const_cast<foo&>(f).modify(); } int main(){ const foo f{2}; test_foo(f); } What

How does volatile work with const?

一个人想着一个人 提交于 2020-07-04 04:27:54
问题 I have this code where as usual, value of the variable "local" stays the same cause it is a const . const int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of local: %d \n", local); Although, when I set local as const volatile , It changes the value of local to 100. Why is that? const volatile int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of

How does volatile work with const?

夙愿已清 提交于 2020-07-04 04:27:11
问题 I have this code where as usual, value of the variable "local" stays the same cause it is a const . const int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of local: %d \n", local); Although, when I set local as const volatile , It changes the value of local to 100. Why is that? const volatile int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of

How does volatile work with const?

…衆ロ難τιáo~ 提交于 2020-07-04 04:26:55
问题 I have this code where as usual, value of the variable "local" stays the same cause it is a const . const int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of local: %d \n", local); Although, when I set local as const volatile , It changes the value of local to 100. Why is that? const volatile int local = 10; int *ptr = (int*)&local; printf("Initial value of local : %d \n", local); *ptr = 100; printf("Modified value of

Why can't I mark this member function as const?

余生长醉 提交于 2020-07-03 19:24:14
问题 When I try to compile this short program: #include <iostream> class Foo { public: friend int getX() const; private: int x; }; int Foo::getX() const { return this->x; } int main() { Foo foo; std::cout << foo.getX() << std::endl; } I get these errors: C:\>gcc test.cpp test.cpp:6:23: error: non-member function 'int getX()' cannot have cv-qualifier friend int getX() const; ^ test.cpp:12:17: error: no 'int Foo::getX() const' member function declared in cl ass 'Foo' int Foo::getX() const { return