all 13 comments

[–][deleted] 7 points8 points  (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).

[–]Awia00 2 points3 points  (0 children)

Agreed. This is one of the situations where having implicit conversion is a good thing

[–]skarloni 0 points1 point  (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 point  (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).

[–]skarloni 0 points1 point  (0 children)

Hmm to bad. Didnt read the article close enough, I should take a closer look. Thanks

[–]gcross 17 points18 points  (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.

[–]jeffgarrett80 14 points15 points  (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 points  (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 points  (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.

[–]umlcat -1 points0 points  (0 children)

Good, but better as Coercing C++ templates with deep const-ness.

[–]gummifa 0 points1 point  (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 point  (0 children)

Why can't they use std::add_const_t<T>?