Template type inference problem
Salih Dincer
salihdb at hotmail.com
Wed Oct 9 08:37:49 UTC 2024
On Wednesday, 9 October 2024 at 07:02:32 UTC, Manu wrote:
>
> I did nothing. I just did work-around and moved on... but I
> think this is
> definitely a bug.
> Just another random edge case in a sea of annoying edge cases!
> :P
If it's not too special, in what application will you use this?
Please consider me as an amateur who is new to D. Because I am a
programmer who almost never uses const. I have a hard time
understanding what the benefits of what you are doing are. Okay,
this code (single template parameter) looks very appealing to me
too:
```d
template S(T)
{
auto S(const(T)[] data, const(T) *sum)
=> Result(data, sum);
struct Result {
const(T)[] data;
const(T) *total;
auto sum() const
=> *total;
string id;
auto toString() const
=> format("%s: %s, %s%s", id, sum,
T.stringof, data);
}
}
import std;
void main()
{
auto arr = [1, 2, 3, 4, 5];
auto sum = arr.sum;
auto s = arr.S(&sum);
s.id = "Sum of the Elements";
s.writeln;// Sum of the Elements: 15, int[1, 2, 3, 4, 5]
++sum;
assert(s.sum == 16);
}
```
But even if sum cannot be changed in the returned Result, since
the data array is already copied, doesn't the fact that it is
const change anything?
SDB at 79
More information about the Digitalmars-d
mailing list