简体   繁体   中英

Template parameter overloading with a non-typename last arg

I'm designing an Array class and wanting to have a number of optional components in a specific order for convenience. Here's my desired header setup:

class Allocator {};
Allocator DefaultAlloc;

class ArrayIterator {};

template<typename Key, typename Value, typename Iterator, Allocator& alloc = DefaultAlloc>
class Array {};

template<typename Key, typename Value, Allocator& alloc = DefaultAlloc>
class Array : public Array<Key, Value, ArrayIterator, alloc> {};

template<typename Value, Allocator& alloc = DefaultAlloc>
class Array : public Array<std::size_t, Valve, alloc> {};

This code won't work, but is there a way to have the same output syntax for class creation? I can't use a parameter pack of types since my last template arg is a non-typename. Is there any way to achieve this with templates or will I have to use unique names for each specialisation?

#include <iostream>

class Allocator {};
Allocator DefaultAlloc;

class ArrayIterator {};

template<
    typename Value, 
    typename Key = std::size_t, 
    typename Iterator = ArrayIterator, 
    Allocator& alloc = DefaultAlloc>
class Array {};

class ArrayIterator2 {};

Array<int> v;
Array<long, int> v2;
Array<long, int, ArrayIterator2> v3;

Just like non-typename parameters, there is no reason typename parameters cannot have default values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM