[cfe-commits] r59232 - /cfe/trunk/lib/Analysis/GRExprEngine.cpp

Zhongxing Xu xuzhongxing at gmail.com
Mon Nov 17 17:44:56 PST 2008


On Tue, Nov 18, 2008 at 6:05 AM, Ted Kremenek <kremenek at apple.com> wrote:

>
> On Nov 16, 2008, at 9:22 PM, Zhongxing Xu wrote:
>
>
>
> On Mon, Nov 17, 2008 at 1:14 PM, Zhongxing Xu <xuzhongxing at gmail.com>wrote:
>
>>
>>
>> On Sun, Nov 16, 2008 at 12:27 PM, Zhongxing Xu <xuzhongxing at gmail.com>wrote:
>>
>>>
>>>
>>> On Sun, Nov 16, 2008 at 11:28 AM, Ted Kremenek <kremenek at apple.com>wrote:
>>>
>>>>
>>>> On Nov 15, 2008, at 5:55 AM, Zhongxing Xu wrote:
>>>>
>>>>
>>>>> One side problem is that I cannot make the MemRegion* in the
>>>>> ImmutableList const. If I did that, I would get compile error, related to
>>>>> FoldingSet Profile() overloading.
>>>>>
>>>>> It seems that ImmutableList cannot take a const type as template
>>>>> parameter, otherwise the  two Profile functions in the partial
>>>>> specialization of FoldingSetTrait would have the same signature.
>>>>> (FoldingSet.h:444, 447).
>>>>>
>>>>
>>>> Could we provide a partial specialization of FoldingSetTrait just for
>>>> ImmutableList?  Or, does (something like) this work (i.e., add another
>>>> partial specialization for const types):
>>>>
>>>> template<typename const T> struct FoldingSetTrait<const T*> {
>>>>  static inline void Profile(const T* X, FoldingSetNodeID& ID) {
>>>>    ID.AddPointer(X);
>>>>  }
>>>> };
>>>>
>>>
>>> Yeah, this works! Thanks. Incidentally, do you think the old problem is
>>> caused by a g++ bug, because, at FoldingSet.h:234, the parameter for Add()
>>> already contains 'const', T should not contain 'const' when being
>>> instantiated.
>>>
>>
>> I was wrong. That was not g++ bug.
>>
>> The template argument const T& need a const type. But const MemRegion* is
>> not a const type,
>> but a non-const pointer to const MemRegion. So T must match the entire
>> 'const MemRegion*'.
>>
>
> If T only matches 'MemRegion*', then the substituted type would be
> 'MemRegion* const', that is a const pointer to non-const MemRegion.
>
> So if we write
>
> template <typename T>
> inline void Add(T const & x) { FoldingSetTrait<T>::Profile(x, *this); }
>
> that would be less confusing.
>
>
> The problem is that we want a 'const MemRegion*' so that Profile()
> operators on constant objects.  If we had:
>
>   const MemRegion* R = ..
>   FoldingSetNodeID& ID;
>   ID.Add(R);
>
> then (I don't believe) that this wouldn't work if we change Add() to take a
> "T const& x" because it would require that the const qualifier in 'const
> MemRegion*' to be cast way.
>

You probably misunderstand me. In fact, 'T const& x' is the same as 'const
T& x'. It's just less confusing, as suggested in Vandevoorde & Josuttis's
C++ Templates, page 4.


>
> Is it possible to add a partial specialization just for pointer types?
>  e.g.:
>
> <template const T*>
> inline void Add(const T* X) { .... }
>
> This seems simpler and also means that the pointer is passed-by-value
> instead of passed-by-reference (which is less error prone).
>

This is possible. But I'm not sure if it is necessary.

Incidentally, it's illegal to write

template <typename const T*>

we cannot have 'const' or '*' modifiers in template parameter.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20081118/c090339a/attachment.html>


More information about the cfe-commits mailing list