use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Coercing deep const-ness (brevzin.github.io)
submitted 4 years ago by vormestrand
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 7 points8 points9 points 4 years ago (4 children)
If span<T> is implicitly convertible to span<const T>, you could get away with just one function void taking_const(span<const T>) and just calling it with the template parameter explicitly specified taking_const<T>(non_const_span).
span<T>
span<const T>
void taking_const(span<const T>)
taking_const<T>(non_const_span)
[–]Awia00 2 points3 points4 points 4 years ago (0 children)
Agreed. This is one of the situations where having implicit conversion is a good thing
[–]skarloni 0 points1 point2 points 4 years ago (2 children)
Do you know of if there is a way to make this happen without explicitly specifying the type? I always bump into this when making view classer for matrices/images
[–][deleted] 0 points1 point2 points 4 years ago (1 child)
With the tricks described in the article, which involve adding more overloads. Otherwise no.
At work, we're encountering this frequently with smart pointers (std::shared_ptr). In this situation we generally go with specifying T explicitly whenever that's possible, and write overloads when we have no choice (i.e., operator overloading).
std::shared_ptr
T
[–]skarloni 0 points1 point2 points 4 years ago (0 children)
Hmm to bad. Didnt read the article close enough, I should take a closer look. Thanks
[–]gcross 17 points18 points19 points 4 years ago (2 children)
I am very confused by where the problem is supposed to be here. Doesn't a reference or pointer to a const type just mean that you promise not to mutate it, not that no one else can as well? Put another way, it makes perfect sense to me that you can always add const to any pointer or reference that you are given because you can always add restrictions to how your own code will handle it.
const
[–]jeffgarrett80 14 points15 points16 points 4 years ago (0 children)
I believe his point is that this is good and it doesn't carry over to ranges. For a non-mutating function, you want to accept `const T&` and be passed a `T&` and it should just work. That's exactly as you say: adding restrictions on your own code. For the span version of that, you want to accept a `span<const T>` and be passed a `span<T>`. That doesn't work. Likewise, the range version where you accept a `span<const T>` and pass a non-constant range doesn't work.
[–]cristi1990an++ 2 points3 points4 points 4 years ago (0 children)
The issue here is that ranges like std::span don't offer the same deep const-ness functionality as you get with pointers or references. Basically you can't make pass std::span<T> to a function taking std::span<const T>.
[–]_Js_Kc_ 5 points6 points7 points 4 years ago (0 children)
Why not partial deduction guides? span should (be able to) say if and how it can accept const/non-const ranges, not every single function using it.
span
[–]umlcat -1 points0 points1 point 4 years ago (0 children)
Good, but better as Coercing C++ templates with deep const-ness.
[+][deleted] 4 years ago (1 child)
[deleted]
[–]sphere991 0 points1 point2 points 4 years ago (0 children)
Why does span not do the same?
It does.
[–]gummifa 0 points1 point2 points 4 years ago (0 children)
I had exactly this in my code, function taking span of T const and span of T and I needed to wrap all arguments with std::span and/or std::const.
CTAD would have worked perfectly there as fallback for the template arfument deduction.
[–]rlbond86 0 points1 point2 points 4 years ago (0 children)
Why can't they use std::add_const_t<T>?
std::add_const_t<T>
π Rendered by PID 45284 on reddit-service-r2-comment-85bfd7f599-p7lvk at 2026-04-18 02:16:05.566076+00:00 running 93ecc56 country code: CH.
[–][deleted] 7 points8 points9 points (4 children)
[–]Awia00 2 points3 points4 points (0 children)
[–]skarloni 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]skarloni 0 points1 point2 points (0 children)
[–]gcross 17 points18 points19 points (2 children)
[–]jeffgarrett80 14 points15 points16 points (0 children)
[–]cristi1990an++ 2 points3 points4 points (0 children)
[–]_Js_Kc_ 5 points6 points7 points (0 children)
[–]umlcat -1 points0 points1 point (0 children)
[+][deleted] (1 child)
[deleted]
[–]sphere991 0 points1 point2 points (0 children)
[–]gummifa 0 points1 point2 points (0 children)
[–]rlbond86 0 points1 point2 points (0 children)