About typequalifier and generic containers.
Jonathan M Davis
jmdavisProg at gmx.com
Sun May 5 14:32:47 PDT 2013
On Sunday, May 05, 2013 18:34:44 =?UTF-8?B?Ikx1w61z?=.Marques
<luismarques at gmail.com>@puremagic.com wrote:
> On Sunday, 5 May 2013 at 16:32:37 UTC, deadalnix wrote:
> > Foo!T do not with Foo!const(T), and for good reasons.
>
> Can you give me an overview of those reasons, please?
Foo!T and Foo!(const T) are completely different and essentially unrelated
types. They're only connection is that they're instantiated from the same
template. You can't assume that anything about them is the same. It's
perfectly possible to do something like
struct Foo(T)
{
static if(is(const(T) == T))
{
string s;
byte b;
auto bar() { return s ~ b; }
}
else
{
T t;
float baz() { return 2.2; }
string toString() { "hello"; }
}
}
So, the compiler can't assume that Foo!T has any relation to Foo!(const T). In
reality, 99.999999% of the time, the only difference would be constness, but
doing nonsense like what that Foo definition does is perfectly possible. With
const T[] and const(T)[], the compiler knows all about how arrays work, so it
knows that converting const T[] to const(T)[] (i.e. make it tail-const) is
perfectly valid, and it just can't do that with user-defined types.
At one point, I tried to make a range's opSlice return a tail-const version of
the range (and alias that to this for implicit conversions), but I ended up
with a recursive template instantiation in the process, which blew the stack
when compiling or somesuch. I _think_ that it could be done by using static if
to break the recursion (though I didn't try that at the time), but I'm not
entirely sure that that can be done, and even if it can, then it's getting
fairly complicated to define a range which can be sliced as tail-const like
arrays can, which likely means that almost no one will do it.
I don't know what the solution is (Steven has some ideas, but I don't know
what they are), but if const and ranges are ever going to mix, we need to be
able to reasonably get tail-const ranges from const ranges. Otherwise, as soon
as you get a const range, it's pretty much useless.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list