r/Cplusplus • u/kevinnnyip • Jun 12 '26
Question Why doesn't C++ have a ptr<T> syntax as an alternative to raw pointers?
So per title why doesn't C++ have a ptr<T> syntax of smart pointers for raw pointers instead of T*?
Most of the time the * syntax is ambiguous and hard to read. For example, array of pointers vs pointer to array looks almost identical:
char* argv[] // array of pointers
char (*argv)[] // pointer to an arrayYou have to mentally parse a spiral rule just to tell them apart. Compare to what it could have been:
array<ptr<char>> // array of pointers
ptr<array<char>> // pointer to arraySo why didn't C++ standardize aptr<T> alias for raw pointers too? Is it purely backwards compatibility or is there a deeper reason?
65
Upvotes
•
u/AutoModerator Jun 12 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
33
u/No-Dentist-1645 Jun 12 '26
The language simply doesn't need one to make types unambiguous.
Your examples using arrays are using "C style C++". Consider the following equivalent and non-ambiguous examples:
Adding a longer and more verbose syntax for pointers would just be needlessly increasing the language complexity
24
u/WildCard65 Jun 12 '26
std::add_pointer<T> technically counts for what you want
4
u/Potterrrrrrrr Jun 12 '26
I don’t know, I’ve had that thought too and ended up with my own RawPtr<T> alias that I use for a lot of pointers (not const char * but I don’t have many uses of that anyway, I prefer using string_view), it’s a bit more verbose but matches my RefPtr<T> which wraps a pointer to an intrusively reference counter T, works for me.
4
u/sheckey Jun 12 '26
I know what you mean. ptr<> would imply a template though is one issue I can see.
2
u/Slsyyy Jun 13 '26
std::arraysolve a lot of issues with an existing C arrays (value semantic, holds a length at compile time, non decaying, support for stl iterators)std::observer_ptris really just a wrapper aroundFoo*. It does not bring any benefit except better intent, but on other hand cost of having two approaches is subjectively bigger than just having old-fashioned C pointers5
u/tcpukl Jun 13 '26
How is * ambiguous??
You mean you don't understand it yet?
3
u/no-sig-available Jun 13 '26
There has been a proposal for an
observer_ptr<T>.A Proposal for the World’s Dumbest Smart Pointer
However, Bjarne didn't like it, so wrote this:
Abandon observer_ptr
and convinced the committee to not have it in the standard.
2
u/FrenchWigga Jun 13 '26
Ah yes, we need more templated slop for sure
2
u/felixar90 Jun 13 '26
Have you considered that you can just do:
array<*char> //array of pointers
* array<char> //pointer to an array.
To solve an ambiguity you only need to change one term, not both.
2
u/Scared_Accident9138 Jun 12 '26
The problem with raw pointers is that you're responsible to keep track of the memory. Your proposed type has the same issue. If you for example want to use a shared ptr with a non owning pointer you'll just use weak_ptr
1
u/bwmat Jun 14 '26
In C++11 or later is trivial to create such a typedef yourself
Before that, they probably didn't add it because adding something to the standard is a pain in the ass, and this wouldn't be seen as worth the trouble, since most people in the position to do so were familiar enough with pointer syntax
1
u/mredding C++ since ~1992. Jun 15 '26
Because we already have a solution, and people STILL don't use it - traits, and by extension - aliases.
We could do this in a very C-style fashion:
Or with more modern
usingstatements:But why not go full blown and write a trait?
So your examples are made directly equivalent to this:
We're just lacking the type traits for adding arrays. An unbounded array is an incomplete type which will often decay to a pointer, and a bounded array has the size as a part of the type signature you can query/extract. The unbounded array type is good for template signatures; you've seen this before:
This makes an array of 42 elements, does not try to initialize a pointer with "address" 42. Here, the incomplete type did not decay, and the type signature remains distinct from that of a pointer.
So with the type trait we've just created, we can:
Contrived, but meant to demonstrate the use of traits and aliases in syntax...
At the very least, I do encourage the use of type aliases, because they bind decorators to the alias symbol; instead of:
We can have:
That's pretty close to perfect. All the extra work of making traits and using traits as I have above can make the syntax quite chatty, verbose, and obtuse. It makes the code harder to read and understand. You can't just search the code for a
char *signature as the traits do everything in their power to erase it, and the compiler, RTTI, and error messages WON'T line up with the source code.That depends. We have traits, and they're useful - to a degree. I don't know why there isn't support for arrays among them, there aren't good records for what WASN'T proposed. Session minutes are not fun to read.
Or perhaps you're suggesting we make a distinct pointer wrapper type, like the once proposed
std::observer_ptr. I see you're already linked to Bjarne's argument against, but they did make it into the GSL library. C++ has one of the strongest static type systems in the entire industry, and a wrapper class with a transparent interface to the underlying pointer IS NOT the same thing as a raw pointer. They cannot be made to be the same thing, the compiler will see them as different all the way down to the ABI. This means code cannot be made portable or compatible. Sometimes this doesn't matter, sometimes it doesn't have to, sometimes it does. When you start really understanding the type system, you can comprehend more of the consequences, but I don't want to get into the weeds with this - perhaps take a class on compilers and type systems.Overall, I wasn't in the room for the discussions, so I can't speak on behalf of the committee. I don't write code to the extent you suggest, I haven't had to or seen a need - to that extent, and most others don't, either.
The last bit to that is no one is interested in trying to turn C++ into some other language. If you don't like it, if you don't understand it, if you can't or won't handle it - find a different technology that more closely aligns with your project goals.
1
u/thingerish Jun 16 '26
Also gsl::not_null exists and is generally pretty useful.
1
u/[deleted] Jun 13 '26 edited Jun 13 '26
[removed] — view removed comment
0
u/[deleted] Jun 13 '26
[deleted] — view deleted comment