Fixing const arrays
kenji hara
k.hara.pg at gmail.com
Sat Dec 10 15:06:53 PST 2011
2011/12/11 Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org>:
> On 12/10/11 4:31 PM, kenji hara wrote:
>>
>> Treating whole constant arrays as ranges by automatically shedding the
>> top-level const is good.
>> But realizing it by language semantic change is definitely bad.It
>> breaks IFTI rule, and adding special case will make difficult to learn
>> language.
>>
>> Instead of language change, we can add specializations that receive
>> non-ranges and convert them to ranges by removing top-level const.
>> I believe that it is Phobos issue and is never the issue of language.
>
>
> I should add there is precedent. C++ also removes top-level const when
> passing objects by value to templates. Deducing top-level const with
> pass-by-value is inherently nonsensical.
>
> Andrei
>
Hmm, it's for sure.
----
void print_type(int){}
template <typename T>
void f(T p)
{
int n;
p = &n; // OK, head is mutable
//*p = 10; // NG, tail is const
print_type(p);
// Error: need explicit cast from int const * to int
// T is deduced as int const * == top const is removed
}
int main()
{
int n;
int const * const p = &n;
f(p);
return 0;
}
Kenji
More information about the Digitalmars-d
mailing list