Pointers have little to no use

Pages: 12345
Agree.

The beauty of C++ is that it is 'C'++. That is to say it supports the low level programming legacy from C.

I think it is this legacy support that gives C++ the edge over other languages like Java (Which I like) for systems development. I like languages that let the developer "Pick their poison" like C++.

I think the new additions to C++ and the STL are really cool, but they should not be added at the expense of legacy. After all, the STL is written in C++.
Well, Stroustrup tends to disagree - in fact he would like to remove many things that come from C but can't. Instead, things we don't like from C can be replaced by things we do like.

I'm not of the stance that pointers should be completely removed from the language, but I am of the stance that a properly designed C++ program should never need to use them. If you need to use pointers, it's a design flaw or you're just being lazy for practical reasons.

And I'm ok with being lazy if you know the scope of your project and think it's pointless to work with higher level constructs. I'm just saying that, if you were to properly use the higher-level constructs when appropriate, you would end up not using raw pointers as a side-effect.
if you were to properly use the higher-level constructs when appropriate, you would end up not using raw pointers as a side-effect.
What to use if you need a non-owning (so unique_ptr won't do), nullable and mutable (so reference won't do), speed critical (so optional won't do) pointer?

Pointers have same amount of importance like inline assembly: sometimes you just need it.
As I've mentioned, I don't consider optimization, so your use of "speed critical" prevents me from answering your question.

Following the paradigm of writing well-design high-level code first and optimizing the bottlenecks to faster, possibly lower-level code later, you would probably end up using std::optional originally and perhaps replacing it with a pointer later.
Nice article.

I mostly agree with you. With C++11 the usage of raw pointers almost ended. But we cant say pointer classes(i am not sure) such as std::unique_ptr is not a pointer. To me they are just wrappers. Very good wrappers.
Last edited on
Will be changed to "can only contain a simple line with a return statement" at c++14.
I was under the impression that it is already that way and C++14 will allow to use loops and primitive local variables here.
I think you've misread both my article and other articles.

I explicitly mentioned the difference between raw pointers and wrappers in my article.

Also, you have that C++11 -> C++14 thing backwards. In C++11, constexpr functions could only contain a return statement, whereas in C++14 they can contain much more. I don't know how you managed to misread info on that.
https://en.wikipedia.org/wiki/C%2B%2B14#Relaxed_constexpr_restrictions
Last edited on
How did i accomplish to misunderstand such a thing. :D:D:D:D

Sorry i just removed it.

I thought the first sentence of your article
In C++, raw pointers have little to no use


was meant for "one should not use raw pointers unless it is the last resort".

And i edited this as "the usage of pointers" -> "the usage of raw pointers". Sorry if that caused the misunderstanding.
Last edited on
Another example of where we are using raw pointers: to temporary hold pointers to memory we should not manage returned by linked C library functons and to pass them to another C functions.
(For managed ones we are using unique_ptr with custom deleter)

But those (that, perfomance one and embedded C++ one) are special clauses. So raw pointers should not be used unless you can prove that all other ways are forbidden to you.
Last edited on
closed account (N36fSL3A)
I remember a few months ago I was discussing why pointers aren't used very much in modern C++ to C# programmers (on minecraftforums, they were stating that C++ is way too low-level and it's features were crap), and they kept insisting that pointers were used everywhere (Despite their little knowledge of the language)...

That's why I'm not debating over there anymore

Sort of off-topic.
Last edited on
You should link those C# programmers to http://meetingcpp.com/index.php/br/items/cpp-status.html
@Fredbill
Yeah, generally, you don't want to waste your time arguing a language with someone that either obviously hates it or has never used it because they will argue blindly that it is either terrible based on outdated features or arguments or all the anti-sentiment they have read.
closed account (N36fSL3A)
Realized that after the third or forth debate I had with them. It's no use.
Last edited on
LB, "just being lazy" implies using the advanced constructs is more work. Isn't the purpose of a HLL to make it less work to develop a program?
closed account (10X9216C)
LB wrote:
Pointers have little to no use.
You should look at std::reference_wrapper ;)

You do realize how reference_wrapper is implemented right? What you mean to say is "I (LB) personally don't like pointer syntax so I am disowning it and using std::reference_wrapper instead".
@medusade: I'm not sure what context you're referring to?

@myesolar: Implementation is out of the question. For all you know, the compiler could implement std::reference_wrapper with blood magic. It doesn't have to use a pointer at all. That just happens to be the most common implementation. Either way, it doesn't matter.
A little off-topic, but still interesting Sean Parent talk about C++ features, use of standard library and program architecture: http://channel9.msdn.com/Events/GoingNative/2013/Cpp-Seasoning
Talk about pointers starts around 48 min mark, but I suggets to watch it from the beginning.
MiiNiPaa: thanks, that was a good watch. His larger, more-encompassing definition of "raw pointer" is weird, but like he said it is very controversial. I don't like how he used shared pointers in his examples though, I feel like things would have made more sense with unique pointers.
Glad you like it. Other interesting talks from first two days of GoingNative2013 are An Effective C++11/14 Sampler by Scott Meyers and Don’t Help the Compiler by STL
How are pointers to pointers handled?
Pages: 12345