const-correctness

How to get alternative value from function that gives wanted data via non-const output parameter for assigning reference-to-const variable to it?

ε祈祈猫儿з 提交于 2019-12-11 12:52:30
问题 The commented code works, but it is not a reference, so it has more computational cost. void CClass::Function(const CArray<CItem*>* ItemsInput) const { /* CArray<CItem*> Items; if (ItemsInput != nullptr) Items.Copy(*ItemsInput); else GetContainer().GetInnerItems(Items, NULL, true); */ const CArray<CItem*>& Items= (ItemsInput!= nullptr)? *ItemsInput : [this] () -> const CArray<CItem*> { CArray<CItem*> InnerItems; GetContainer().GetInnerItems(InnerItems, NULL, true); return const_cast <const

Initializing non-const parameter with string literal

只愿长相守 提交于 2019-12-10 15:04:51
问题 So I have this code: class ConstTest { public: explicit ConstTest(char* name) {} }; int main() { ConstTest t("blarghgh"); } It obviously compiles, even though I thought that it shouldn't. As string literals in C++ have type const char[] , and ConstTest constructor requires a const-less char* — not const char* . And casting a const pointer to a non-const one isn't something usually done by C++ implicitly. So, where I'm wrong? Why it's compiling? Can I legally modify the dereferenced pointer

Converting from “foo<T>” to “const foo<const T>” - C++

若如初见. 提交于 2019-12-10 14:39:50
问题 I have a function like (please don't care about returning temporary by reference. This is just an example to explain the problem), const foo<const int>& get_const() { foo<int> f; return f; } This obviously won't compile. I am looking for a way to ensure callers won't change the T of foo . How can I ensure that? I have seen the similar behavior for boost::shared_ptr . shared_ptr<T> is convertible to const shared_ptr<const T> . I couldn't figure out how it is doing this. Any help would be great

Const correctness causing problems with containers for pointers?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 11:33:13
问题 Given this code (C++, Qt containers are used but I suppose the question is universal): // a containter for Item-s QList<Item*> items; // argument is const to prevent changing the item by this function void doStuff(const Item *item) { // find index of the item inside the container // indexOf() is declared as: // template <typename T> int QList<T>::indexOf(const T &t, int from = 0) const const int itemIndex = items->indexOf(item); } I get a compile error (MSVC2010): error C2664: 'QList::indexOf

boost::optional not letting me reassign const value types

大兔子大兔子 提交于 2019-12-10 04:32:21
问题 It seems to me there should be four variants of boost::optional optional<Foo> => holds a mutable Foo and can be reassigned after initialization optional<Foo const> const => holds a const Foo and can't be reassigned after initialization optional<Foo> const => (should?) hold a mutable Foo but can't be reassigned after initialization optional<Foo const> => (should?) hold a const Foo and can be reassigned after initialization The first 2 cases work as expected. But the optional<Foo> const

How to generate a non-const method from a const method?

岁酱吖の 提交于 2019-12-09 05:30:06
问题 While striving for const-correctness, I often find myself writing code such as this class Bar; class Foo { public: const Bar* bar() const { /* code that gets a Bar somewhere */ } Bar* bar() { return const_cast< Bar* >( static_cast< const Foo* >(this)->bar()); } }; for lots of methods like bar() . Writing these non-const methods which call the const ones by hand is tedious; besides, I feel I am repeating myself – which makes me feel bad. What can I do to alleviate this task? (Macros and code

const correctness for structs with pointers

Deadly 提交于 2019-12-08 23:47:23
问题 I have a struct which contains some pointers. I want the value of these to be unmodifiable. But simply writing const infront doesn't make the structs members unmutable typedef struct{ int *x; int *y; }point; void get(const point *p,int x, int y){ p->x[0]=x;//<- this should not be allowed p->y[0]=y;//<- this should not be allowed } Can someone point me in the right direction. EDIT: So it would seem that there is no simple way of using the function prototype to tell that everything belonging to

Why am I getting an error converting a ‘float**’ to ‘const float**’?

牧云@^-^@ 提交于 2019-12-08 07:04:30
I have a function that receives float** as an argument, and I tried to change it to take const float** . The compiler ( g++ ) didn't like it and issued : invalid conversion from ‘float**’ to ‘const float**’ this makes no sense to me, I know (and verified) that I can pass char* to a function that takes const char* , so why not with const float** ? Sean See Why am I getting an error converting a Foo** → const Foo**? Because converting Foo** → const Foo** would be invalid and dangerous ... The reason the conversion from Foo** → const Foo** is dangerous is that it would let you silently and

How to provide multiple begin/end proxies for a class

一笑奈何 提交于 2019-12-08 04:58:58
问题 Given the classes struct Data { void bar() const; void baz(); } class Foo { std::vector<Data> data; std::map<size_t, Data> indexed_data; } I'd like to implement something in class Foo so that I can do the following: int main() { Foo foo; for(const auto& data : foo.data()) data.bar(); for(auto& data : foo.indexed_data()) data.baz(); const auto& foo_ref = foo; for(auto& data : foo_ref.data()) data.baz(); // constness violated, shouldn't compile } However, I don't wanna expose the class

Const-correctness and hardware writes

房东的猫 提交于 2019-12-07 05:12:24
问题 Say I have the following member function: void CFoo::regWrite( int addr, int data ) { reg_write( addr, data ); // driver call to e.g. write a firmware register } Clearly, calling this function doesn't modify the internal state of the object it is called on. However, it changes the state of whatever this Foo instance represents . In circumstances such as these, should Foo::regWrite(int addr, int data) be a const function? 回答1: You have to decide what the meaning is of "logically const" for the