4

Silly question, but say you have class Foo:

class Foo { public: typedef boost::shared_ptr<Foo> RcPtr; void non_const_method() {} void const_method() const {} }; 

Having a const Foo::RcPtr doesn't prevent non-const methods from being invoked on the class, the following will compile:

#include <boost/shared_ptr.hpp> int main() { const Foo::RcPtr const_foo_ptr(new Foo); const_foo_ptr->non_const_method(); const_foo_ptr->const_method(); return 0; } 

But naming a typedef ConstRcPtr implies, to me, that the typedef would be

typedef const boost::shared_ptr<Foo> ConstRcPtr; 

which is not what I'm interested in. An odder name, but maybe more accurate, is RcPtrConst:

typedef boost::shared_ptr<const Foo> RcPtrConst; 

However, Googling for RcPtrConst gets zero hits, so people don't use this as a typedef name :)

Does anyone have any other suggestions?

3 Answers 3

4

The name of a typedef does not represent the syntactical construct used to define its type. The typedef name should convey some of its desired meaning. For example, the Standard defines the names of iterators over const T as const_iterator, even though the iterator itself is not const (you can still increment it). In fact, the whole purpose of an iterator is to change it, in order to iterate over a sequence :)

typedef T const* const_iterator; 

For pointers, i personally see little reason to generally typedef them as constant. So the Const in front of the trailing parts could convey the same meaning as the iterators do, and will go with existing practice. So i think it is perfectly fine for you to go and say

typedef boost::shared_ptr<Foo const> ConstRcPtr; 

In fact, this is how std::allocator<>::const_pointer is worded too.

Sign up to request clarification or add additional context in comments.

2 Comments

"Admittedly the standard library contains the concept of a const_iterator that is, unforgivably, an iterator that refers to constant elements; the iterator itself is not constant.(Just because the standards committee has a bad day doesn't mean you should emulate them.)" -- Stephen C. Dewhurst C++ Gotchas
@tod imho it is not a bad idea
4

Your statement about where the const goes is right: You need another typedef. In our project, we use Foo::TPtr and Foo::TConstPtr as standard typedefs.

Comments

1

naming a typedef ConstRcPtr implies, to me, that the typedef would be typedef const boost::shared_ptr<Foo> ConstRcPtr;

Why does it imply that? When people talk about "const pointers" (as opposed to non-const pointers), they almost always are referring to const char*, not char *const. const_iterator is an iterator-to-const, not an iterator which is const, and I would not be at all surprised that the relationship between ConstRcPtr and RcPtr is the same as the relationship between const_iterator and iterator.

If you need to distinguish between const char* and char *const, then yes, you might call char *const a "const pointer" and const char* a "pointer-to-const". But you almost never do, and so the short phrase "const pointer" is used for the common case. I think that applies here too, unless you're planning to typedef all four of:

boost::shared_ptr<Foo> boost::shared_ptr<const Foo> const boost::shared_ptr<Foo> const boost::shared_ptr<const Foo> 

In that case you'll have to be a bit more creative with names.

6 Comments

I'm not planning on typedef'ing the const boost::shared_ptr<> itself, since that can be done where the typedef is used. I need the const Foo as an argument type to indicate that the method will not modify the pointed to object.
"Colloquially, many programmers refer to pointers to constant data "const pointers." This is not a good idea, because it communicates the correct meaning (pointer to constant data) only to ignoramuses, and will mislead any competent C++ programmer who takes you at your word (a constant pointer to non-constant data)." - Stephen C. Dewhurst C++ Gotchas I'm through quoting now I promise, I was just rereading this section of his book the day before I came across this discussion and couldn't resist.
@Tod: I think Stephen C. Dewhurst is mistaken. I agree that it would be more clear if people always said "pointer-to-const", and if const_iterator were called iterator_to_const. I'm clearly not an ignoramus, since I'm aware of the issue. So I disagree with his claim that only ignoramuses can ever understand what those inaccurate phrases mean when they're used in practice. I also suspect he's being a bit disingenuous when he claims that no competent C++ programmer is capable of understanding those terms. I reckon he's accidentally put his "someone's wrong on the internet!" rant in a book.
+1 on your response but in his defense he did specify a competent programmer that "takes you at your word." I think he wanted to attempt a turn back to literal meaning but by 2003 (the copyright date) I suspect idiomatic usage was already too well entrenched. I'm new to smart ptrs and struggling with the typedef naming thing, it all sounds like it has a Hungarian accent to me, and I'm leaning towards no "ptr" or "const" in the names.
I certainly agree with him that it's not a good idea to call pointers-to-const "const pointers", because that's a phrase that literally means something different. But it has become common enough to be comprehensible. I disagree with him that it's unforgivable (or even that it's wrong) to call iterators-to-const const_iterator, because that's a token that that standard gets to define, and nobody else should want to refer to a const vector<int>::iterator as a const_iterator.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.