Topic: Template template concepts


Author: REkalleMOunderscoreVErutanenME@hotmail.com (Kaba)
Date: Thu, 12 Apr 2007 16:53:05 GMT
Raw View
Continuing with concepts..

Suppose I have a 2-dimensional array, for which I have two classes, both
modeling an Array2Concept:

template <typename Type>
class LinearArray2;

template <typename Type>
class TiledArray2;

Currently, using just one of these classes one would write functions of
the form:

template <typename Type>
void clear(LinearArray2<Type>& that, const typename Identity
<Type>::Result& value);

(The Identity is used to suppress the parameter deduction for the second
argument, so that Type is deduced from the template parameter of the
first argument).

Suppose I wish to generalize the function to taking an object modeling
the Array2Concept. I approach this as follows:

template <template <typename> class Array2, typename Type>
void clear(Array2<Type>& that, const Identity<Type>::Result& value);

I know I could have simply made Array2 a typename, but I really dislike
that style..

Old eyes regocnize there are problems with genericy here
* Array2 now explicitly states to require exactly one template
parameter. That's something that could be corrected by variadic
templates.
* Thinking about the future, one would need to do concept maps for
native types also (well, this is a bad example w.r.t. that..), so
classes don't work here in general.

Now, move on to the C++09 domain. There, as I see it, I must use
associated types:

template <Array2Concept Array2>
void clear(Array2& that, const Array2::value_type& value);

But there are some problems here:
* I need to introduce a new typedef to all classes modeling Array2
concept, the value_type.
* I really liked the earlier form more beautiful.

So maybe I could do something like this (I am not sure how and if the
variadic templates would work here):

template <template <typename, ...> class Array2, typename Type>
requires Array2Concept<Array2<Type, ...> >
void clear(Array2<Type, ...>& that, const Identity<Type>::Result&
value);

That's almost everything I want, but the problem with the native types
still remains. A term that runs to my mind is something like a template
template concept:

template <template <typename, ...> Array2Concept Array2, typename Type>
void clear(Array2<Type, ...>& that, const Identity<Type>::Result&
value);

But how would the compiler deduce Type now..?

Anyway, I'd be happy to see a solution that mimiced the situation with
the LinearArray2.

--
Kalle Rutanen
http://kaba.hilvi.org

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: REkalleMOunderscoreVErutanenME@hotmail.com (Kaba)
Date: Fri, 13 Apr 2007 17:10:32 GMT
Raw View
> template <template <typename, ...> Array2Concept Array2, typename Type>
> void clear(Array2<Type, ...>& that, const Identity<Type>::Result&
> value);
>
> But how would the compiler deduce Type now..?
>
> Anyway, I'd be happy to see a solution that mimiced the situation with
> the LinearArray2.

Now that I think of it, I really can't see a way for a compiler to match
the template parameters of a concept from a given type. It seems to be
essential that the type itself contains all the information and not so
that some information is deduced by pattern matching.

My complaint is purely a style or beauty issue. Maybe I just have to
bite the bullet:)

--
Kalle Rutanen
http://kaba.hilvi.org

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]