const-correctness

“const correctness” in C#

淺唱寂寞╮ 提交于 2019-12-27 13:37:21
问题 The point of const-correctness is to be able to provide a view of an instance that can't be altered or deleted by the user. The compiler supports this by pointing out when you break constness from within a const function, or try to use a non-const function of a const object. So without copying the const approach, is there a methodology I can use in C# that has the same ends? I'm aware of immutability, but that doesn't really carry over to container objects to name but one example. 回答1: I've

Cannot apply const to typedef reference

自作多情 提交于 2019-12-22 08:37:36
问题 The following code works when applying const to a return value reference of value_type& but errors if I use a typedef of the same type. As an example: class T { }; class A { public: typedef T value_type; typedef value_type& reference; // Not working const reference operator*() const; // But this works? //const value_type& operator*() const; }; // Error! const typename A::reference A::operator*() const { } int main() { return 0; } g++ will error with: 'const' qualifiers cannot be applied My

How would a heap-allocated const object differ from non-const one?

不问归期 提交于 2019-12-22 03:40:24
问题 In C++ it is possible to allocate a const object on heap: const Class* object = new const Class(); const_cast<Class*>( object )->NonConstMethod(); // UB so that attempt to write into an object will be UB. I don't get how such an object will be different from a heap-allocated object that is not declared const : const Class* object = new Class(); I mean when I allocate an object on stack it goes to automatic storage which is implementation-specific and so there might be some implementation

Is const_cast<const Type*> ever useful?

岁酱吖の 提交于 2019-12-21 08:27:05
问题 Recently I found a piece of C++ code that effectively does the following: char* pointer = ...; const char* constPointer = const_cast<const char*>( pointer ); Obviously the author thought that const_cast means "add const", but in fact const can be just as well added implicitly: const char* constPointer = pointer; Is there any case when I would really have to const_cast to a pointer-to-const ( const_cast<const Type*> as in above example)? 回答1: const_cast , despite its name, is not specific to

How to deal with initialization of non-const reference member in const object?

血红的双手。 提交于 2019-12-21 04:42:14
问题 Let's say you have a class class C { int * i; public: C(int * v):i(v) {}; void method() const; //this method does not change i void method(); //this method changes i } Now you may want to define const instance of this class const int * k = whatever; const C c1(k); //this will fail but this will fail because of non-const int C's constructor C(int * v) so you define a const int constructor C(const int * v):i(v) {}; //this will fail also But this will fail also since C's member "int * i" is non

Does this code subvert the C++ type system?

本小妞迷上赌 提交于 2019-12-20 17:29:32
问题 I understand that having a const method in C++ means that an object is read-only through that method, but that it may still change otherwise. However, this code apparently changes an object through a const reference (i.e. through a const method). Is this code legal in C++? If so: Is it breaking the const -ness of the type system? Why/why not? If not: Why not? Note 1: I have edited the example a bit, so answers might be referring to older examples. Edit 2: Apparently you don't even need C++11,

Write-Only pointer type

亡梦爱人 提交于 2019-12-20 08:33:30
问题 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 registers will produce undefined values when read. I want to define a pointer type that will allow the compiler to detect when reading values from a write-only registers (a.k.a. dereferencing). Can a write-only pointer be created using only C language syntax? (We are developing first prototype using C, but

Can an object know its own constness?

守給你的承諾、 提交于 2019-12-19 05:34:28
问题 With decltype and std::is_const the constness of a variable can be externally detected. But is it also possible for an object to know its own constness? Usage should be like: #include <type_traits> #include <iostream> #include <ios> struct Test { Test() {} bool print() const { // does not work as is explained in https://stackoverflow.com/q/9890218/819272 return std::is_const<decltype(*this)>::value; // <--- what will work?? } }; int main() { Test t; const Test s; // external constness test

Can an object know its own constness?

偶尔善良 提交于 2019-12-19 05:34:11
问题 With decltype and std::is_const the constness of a variable can be externally detected. But is it also possible for an object to know its own constness? Usage should be like: #include <type_traits> #include <iostream> #include <ios> struct Test { Test() {} bool print() const { // does not work as is explained in https://stackoverflow.com/q/9890218/819272 return std::is_const<decltype(*this)>::value; // <--- what will work?? } }; int main() { Test t; const Test s; // external constness test

implementing a variadic zip function with const-correctness

北城以北 提交于 2019-12-19 03:44:06
问题 I'm trying to implement a zip function. zip 's parameters are each wrapped<Ti> , where Ti varies from parameter to parameter. zip takes these wrapped<Ti> s and produces a wrapped<tuple<T1&,T2&,...TN&>> , or in other words a wrapped tuple of references to its parameters. The references should preserve const -ness. Here's my first stab at zip with one parameter, which doesn't work in general: #include <utility> #include <tuple> // implement forward_as_tuple as it is missing on my system