const-correctness

Converting from "foo<T>" to "const foo<const T>" - C++

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...

const correctness and return values - C++

Please consider the following code. struct foo { }; template<typename T> class test { public: test() {} const T& value() const { return f; } private: T f; }; int main() { const test<foo*> t; foo* f = t.value(); return 0; } t is a const variable and value() is a constant member-function...

Using traits in C++

This question is related to my last one. I am trying to solve the problem using traits<T> and traits<T*>. Please consider the following code. template<typename T> struct traits { typedef const T& const_reference; }; template<typename T> struct traits<T*> { typedef const T const_reference; }; template<typename T> class test {...

Using boost::optional with constant types - C++

I have a container class which uses boost::optional to hold the value. Here is the code looks like, template<typename T> struct traits { typedef T value_type; typedef T& reference; }; template<typename T> struct traits<const T> { typedef const T value_type; typedef const T& reference; }; template<typename T> struct t...

Const-Correctness on complex return value

Hello! Code first: struct Foo { char * DataPtr; }; class ISomeInterface { public: Foo GetFoo( ) const; Foo GetFoo( ); }; The Foo::DataPtr is a pointer to an internal buffer of the object behing ISomeInterface. Is there a way to make sure that the Foo::DataPtr returned by the const version of ISomeInterface::GetFoo is a c...

How often do you declare your functions to be const?

Do you find it helpful? ...

C++ operator + and * non-const overloading

I have the following tricky problem: I have implemented a (rather complicated) class which represents mathematical functions in a multiwavelet basis. Since operations like +, - and * are quite natural in this context, I have implemented overloaded operators for this class: FunctionTree<D> operator+(FunctionTree<D> &inpTree); FunctionTre...

Does it ever make sense to make a fundamental (non-pointer) parameter const?

I recently had an exchange with another C++ developer about the following use of const: void Foo(const int bar); He felt that using const in this way was good practice. I argued that it does nothing for the caller of the function (since a copy of the argument was going to be passed, there is no additional guarantee of safety with reg...

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

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 discuss...

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

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 f...

How to return a 'read-only' copy of a vector

Hi, I have a class which has a private attribute vector rectVec; class A { private: vector<Rect> rectVec; }; My question is how can I return a 'read-only' copy of my Vector? I am thinking of doing this: class A { public: const vect<Rect>& getRectVec() { return rectVect; } } Is that the right way? I am thinking this can guard...

RAII: Initializing data member in const method

In RAII, resources are not initialized until they are accessed. However, many access methods are declared constant. I need to call a mutable (non-const) function to initialize a data member. Example: Loading from a data base struct MyClass { int get_value(void) const; private: void load_from_database(void); // Loads the ...

Modifying a const through a non-const pointer

I'm a bit confused what happened in the following code: const int e = 2; int* w = ( int* ) // (1) cast to remove const-ness *w = 5; // (2) cout // (3) outputs 5 cout // (4) outputs 2 cout // (5) w points to the address of e cout In (1), w points to the address of e. In...

Const Functions and Interfaces in C++

I'll use the following (trivial) interface as an example: struct IObject { virtual ~IObject() {} virtual std::string GetName() const = 0; virtual void ChangeState() = 0; }; Logic dictates that GetName should be a const member function while ChangeState shouldn't. All code that I've seen so far doesn't follow this logic, though...

C++ Pass By Const Reference and Return By Const Reference

I'm trying to understand if there is any benefit to returning a const reference. I have a factorial function that normally looks like this: unsigned long factorial(unsigned long n) { return (n == 0) ? 1 : n * factorial(n - 1); } I'm assuming that there will be a performance increase when we pass by const reference and we return a ...

C++ : automatic const?

When I compile this code: class DecoratedString { private: std::string m_String; public: // ... constructs, destructors, etc std::string& ToString() const { return m_String; } } I get the following error from g++: invalid initialization of reference of type 'std::string&" from expression of type 'const...

Can const-correctness improve performance?

I have read numerous times that enforcing const-correctness in your C or C++ code is not a good practice with regards to maintainability, but also it may allow your compiler to perform optimizations. However, I have read the complete opposite, too that it does not affect performance at all. Therefore, do you have examples where const c...

Const method that modifies *this without const_cast

The following pattern has arisen in a program I'm writing. I hope it's not too contrived, but it manages to mutate a Foo object in the const method Foo::Questionable() const, without use of any const_cast or similar. Basically, Foo stores a reference to FooOwner and vice versa, and in Questionable(), Foo manages to modify itself in a c...

Const pointer argument to a method which delegates to removeAll()

Consider a method like this: void Parent::removeChild(Child *child) { children.removeAll(child); } In this case, since child is never modified itself, one could make it a const pointer. But since children is of the type QList, the removeAll() takes a const reference to a non-const pointer. What's the recommended way to handle thi...

How do you initialise an array of const values in D2?

Essentially, I want to be able to do something like this: struct Foo { const(int)[2] ints; this(int x, int y) { ints = [x, y]; } } but this doesn't work. The compiler (DMD 2.048) just complains that ints isn't mutable. How are you supposed to initialise the array? ...