const-correctness

C++ Overloading Conversion Operators

*爱你&永不变心* 提交于 2019-12-04 21:47:41
问题 I am trying to have a class that allows implicit casting to certain built in types, like unsigned long int and since I'm trying to do this as correct as possible (this is my first important project in C++), I have hit a strange issue regarding const correctness: This works: #include <iostream> class CustomizedInt { private: int data; public: CustomizedInt(); CustomizedInt(int input); operator unsigned long int () const { unsigned long int output; output = (unsigned long int)data; return

Is it legal to modify a dynamically-allocated `const` object through a re-used non-`const` name?

此生再无相见时 提交于 2019-12-04 21:41:36
问题 Consider the following program: #include <iostream> int main() { int x = 0; const int* px = new (&x) const int(0); x = 1; std::cout << *px; // 1? } It compiles under GCC 4.8 (and produces the "expected" output), but I suspect it's entirely UB because the dynamic object has type const int (which remains part of the type). But, then, if so why isn't the compiler stopping me from violating const -correctness? 回答1: tl;dr: Yes, it's undefined behavior. No, the compiler doesn't diagnose it. In

When should a member function have a const qualifier and when shouldn't it?

别说谁变了你拦得住时间么 提交于 2019-12-04 09:32:45
问题 About six years ago, a software engineer named Harri Porten wrote this article, asking the question, "When should a member function have a const qualifier and when shouldn't it?" I found it to be the best write-up I could find of the issue, which I've been wrestling with more recently and which I think is not well covered in most discussions I've found on const correctness. Since a software information-sharing site as powerful as SO didn't exist back then, I'd like to resurrect the question

Is const_cast<const Type*> ever useful?

谁说胖子不能爱 提交于 2019-12-04 01:43:44
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)? const_cast , despite its name, is not specific to const ; it works with cv-qualifiers which effectively comprises both const and volatile . While adding such a

Const vector of non-const objects

余生颓废 提交于 2019-12-03 10:50:16
问题 In defining a function in an interface : virtual void ModifyPreComputedCoeffs ( std::vector < IndexCoeffPair_t > & model_ ) = 0; we want to specify that the vector model_ should not be altered in the sense push_back etc operations should not be done on the vector, but the IndexCoeffPair_t struct objects in the model_ could be changed. How should we specify that ? virtual void ModifyPreComputedCoeffs ( const std::vector < IndexCoeffPair_t > & model_ ) = 0; does not work I think. 回答1: The C++

Does this code subvert the C++ type system?

穿精又带淫゛_ 提交于 2019-12-03 04:47:00
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, so I removed that dependency. #include <iostream> using namespace std; struct DoBadThings { int *p;

When should a member function have a const qualifier and when shouldn't it?

风流意气都作罢 提交于 2019-12-03 03:23:44
About six years ago, a software engineer named Harri Porten wrote this article , asking the question, "When should a member function have a const qualifier and when shouldn't it?" I found it to be the best write-up I could find of the issue, which I've been wrestling with more recently and which I think is not well covered in most discussions I've found on const correctness. Since a software information-sharing site as powerful as SO didn't exist back then, I'd like to resurrect the question here. The article seems to cover a lot of basic ground, but the author still has a question about const

Const vector of non-const objects

泄露秘密 提交于 2019-12-03 01:19:22
In defining a function in an interface : virtual void ModifyPreComputedCoeffs ( std::vector < IndexCoeffPair_t > & model_ ) = 0; we want to specify that the vector model_ should not be altered in the sense push_back etc operations should not be done on the vector, but the IndexCoeffPair_t struct objects in the model_ could be changed. How should we specify that ? virtual void ModifyPreComputedCoeffs ( const std::vector < IndexCoeffPair_t > & model_ ) = 0; does not work I think. The C++ const-correctness concept is IMO way overrated. What you just discovered is one of the big limitations it has

std::vector of objects and const-correctness

最后都变了- 提交于 2019-12-02 23:05:15
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 main(int argc, char *argv[]) { A foo(1337); vector<A> vec; vec.push_back(foo); // <-- compile error! return 0; } Obviously, the copy constructor is not enough. What am I missing? EDIT: Ofc. I cannot change this->c in operator=() method, so I don't see how operator=() would be used (although required by std::vector). I'm not sure why nobody said it, but the correct answer is to drop the const , or store A* 's in the vector

Write-Only pointer type

為{幸葍}努か 提交于 2019-12-02 16:07:13
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 moving to C++ on 2nd generation.) How can an efficient write-only pointer be created in C++? (Remember,