Rafael Adel Rafael Adel - 1 year ago 117
C++ Question

C++ - creating an alias for an std::array of std::array iterator

I have an array of iterators of type

std::array<Point, SIZE>::iterator
, where
is a template variable.

So having an array of these iterators would be

std::array<std::array<Point, SIZE>::iterator, SIZE>

which is a pain to write in the number of functions that I have.

I was thinking of aliasing this type, but I don't know how, while having template variable

I tried

template<std::size_t SIZE>
using p_iterators = std::array<std::array<Point, SIZE>::iterator, SIZE>;

And inside the function I did :

template<std::size_t SIZE>
template <typename T>
p_iterators<SIZE> eucledian_closest(T &points) {}

I got these errors:

closest_pair.cpp:24:70: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, long unsigned int _Nm> struct std::array’
using p_iterator = std::array<std::array<Point, SIZE>::iterator, SIZE>;
closest_pair.cpp:24:70: error: expected a type, got ‘std::array<Point, SIZE>::iterator’
closest_pair.cpp:42:1: error: ‘p_iterator’ does not name a type
p_iterator<SIZE> eucledian_closest(T &points) {

I don't know how to alias such a type or even how to use it with that template while having another template in the function
template <typename T>

Answer Source

In C++, dependent template type names require the keyword, typename.

You should change your alias from

template<std::size_t SIZE>
using p_iterators = std::array<std::array<Point, SIZE>::iterator, SIZE>;


template<std::size_t SIZE>
using p_iterators = std::array<typename std::array<Point, SIZE>::iterator, SIZE>;

Then change the template function signature to this:

template<std::size_t SIZE, typename T>
p_iterators<SIZE> eucledian_closest(T &points) {}

In the above case, when calling the function, eucledian_closest, you will at least need to explicitly provide the SIZE parameter like eucledian_closest<3>(myArry). Without doing so, type deduction will fail to deduce SIZE, plus the down side is that you should have the size to match. If you want type deduction to deduce it implicitly SIZE for you:

You can do something like:

template<template <typename, std::size_t> class Array,
                                          typename T,
                                          std::size_t SIZE
p_iterators<SIZE> eucledian_closest(Array<T, SIZE>& points) {}

With the above, you can do eucledian_closest(myArray). However, if you do not need to use SIZE in the function, you could simply do

template<typename T>
auto eucledian_closest(T& points) {}

And simply call it like eucledian_closest(myArray).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download