Topic: Template Aliases, Specialization, and Concepts
Author: Scott Meyers <usenet@aristeia.com>
Date: Fri, 22 May 2009 02:07:24 CST Raw View
14.5/3 of N2800 notes that template aliases cannot be specialized, and
N1489 shows how to achieve the same effect using traits classes. If I
want to have a template alias for a vector with an allocator of my
choice, where I have different allocators for pointer and non-pointer
types, I can do this:
template<typename T> // primary
struct VecAllocator { // template
typedef MyAllocator type;
};
template<typename T> // specialized
struct VecAllocator<T*> { // template
typedef MyPtrAllocator> type;
};
template<typename T>
using MyAllocVec = std::vector<T, VecAllocator<T>::type>;
My knowledge of concepts is sketchy, but I'm wondering if I could
achieve the same effect by overloading my alias on concepts and thus
get rid of the traits class. Could I?
Thanks,
Scott
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: daniel.kruegler@googlemail.com
Date: Fri, 22 May 2009 11:35:27 CST Raw View
On 22 Mai, 10:07, Scott Meyers <use...@aristeia.com> wrote:
> 14.5/3 of N2800 notes that template aliases cannot be specialized, and
> N1489 shows how to achieve the same effect using traits classes. If I
> want to have a template alias for a vector with an allocator of my
> choice, where I have different allocators for pointer and non-pointer
> types, I can do this:
>
> template<typename T> // primary
> struct VecAllocator { // template
> typedef MyAllocator type;
> };
>
> template<typename T> // specialized
> struct VecAllocator<T*> { // template
> typedef MyPtrAllocator> type;
> };
>
> template<typename T>
> using MyAllocVec = std::vector<T, VecAllocator<T>::type>;
You need the typename here
template<typename T>
using MyAllocVec = std::vector<T, typename VecAllocator<T>::type>;
but otherwise this alias definition is correct.
> My knowledge of concepts is sketchy, but I'm wondering if I could
> achieve the same effect by overloading my alias on concepts and thus
> get rid of the traits class. Could I?
There is currently an active issue about this, see
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#851
Even though the grammar would currently allow such a
constrained form of an alias, the tendency is to consider
this as an artifact of the current spec.
Or did you mean something differently here?
Greetings from Bremen,
Daniel
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Scott Meyers <usenet@aristeia.com>
Date: Sat, 23 May 2009 11:49:01 CST Raw View
daniel.kruegler@googlemail.com wrote:
>>
>> template<typename T>
>> using MyAllocVec = std::vector<T, VecAllocator<T>::type>;
>
> You need the typename here
Duh, right, thanks.
>> My knowledge of concepts is sketchy, but I'm wondering if I could
>> achieve the same effect by overloading my alias on concepts and thus
>> get rid of the traits class. Could I?
>
> There is currently an active issue about this, see
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#851
>
> Even though the grammar would currently allow such a
> constrained form of an alias, the tendency is to consider
> this as an artifact of the current spec.
>
> Or did you mean something differently here?
I think that's what I meant. In general, I view traits as a
roundabout way of expressing things, so if there is a more
straightforward mechanism (e.g., concepts), I'd prefer that.
Something like:
template<PointerType T>
using MyAllocVec = std::vector<T, MyPtrAllocator<T>>;
template<typename T>
using MyAllocVec = std::vector<T, MyAllocator<T>>;
Scott
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]