0

The following code doesn't compile on the latest Microsoft Visual Studio. Could someone enlighten me on what I'm doing wrong here?

#include <iostream>
#include <iomanip>
#include <array>

template <typename T, std::size_t M, std::size_t N>
using Matrix = std::array<T, M * N>;

template <typename T, std::size_t M, std::size_t N>
std::ostream &operator<<(std::ostream &os, const Matrix<T, M, N> &matrix)
{
    for (auto i = 0; i < M; ++i)
    {
        for (auto j = 0; j < N; ++j)
        {
            os << std::setw(5) << matrix[i * N + j];
        }

        os << std::endl;
    }

    return os;
}

int main(int argc, const char * const argv[])
{
    Matrix<float, 2, 3> matrix{
        1.1f, 1.2f, 1.3f,
        2.1f, 2.2f, 2.3f
    };

    std::cout << matrix << std::endl;

    return 0;
}

Here is a snapshot of the compiler error:

1>main.cpp(30): error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::array<T,6>' (or there is no acceptable conversion)
1>          with
1>          [
1>              T=float
1>          ]

Edit: The following code works though:

#include <iostream>
#include <iomanip>
#include <array>

template <typename T, std::size_t M, std::size_t N>
using Matrix = std::array<std::array<T, N>, M>;

template <typename T, std::size_t M, std::size_t N>
std::ostream &operator<<(std::ostream &os, const Matrix<T, M, N> &matrix)
{
    for (auto row : matrix)
    {
        for (auto element : row)
        {
            os << std::setw(5) << element;
        }

        os << std::endl;
    }

    return os;
}

int main(int argc, const char * const argv[])
{
    Matrix<float, 2, 3> matrix{
        1.1f, 1.2f, 1.3f,
        2.1f, 2.2f, 2.3f
    };

    std::cout << matrix << std::endl;

    return 0;
}
0ca6ra
  • 63
  • 3
  • 4
    With `N*M` in the template parameter of your `operator<<` (indirectly via `std::array`), it is impossibly to deduce `N` and `M` independently. Note that instantiations of type aliases are equivalent to the type they refer to, that is, your second `operator<<` parameter is equivalent to `const std::array &matrix` – dyp Oct 12 '15 at 17:00
  • 1
    @dyp, May be you should make it an answer – Lol4t0 Oct 12 '15 at 17:19
  • thanks, i see it clearly now! – 0ca6ra Oct 12 '15 at 17:19
  • similar [C++11 alias templates in CUDA](http://stackoverflow.com/q/33010352/3953764) – Piotr Skotnicki Oct 12 '15 at 17:40

1 Answers1

2

Bearing in mind @dyp's comment what you have to do here is to create the new type instead of alias that will have 2 independent params.

So you just use aggregation including actual data as a field, like:

template <typename T, std::size_t M, std::size_t N>
class Matrix
{
private:
    std::array<T, M * N> _data;
    template <typename T1, std::size_t M1, std::size_t N1> friend std::ostream &operator<<(std::ostream &os, const Matrix<T1, M1, N1> &matrix);
public:
    template <typename...Args>
    Matrix(Args... args):
        _data{{std::forward<Args>(args)...}}
    {}
};
Lol4t0
  • 12,444
  • 4
  • 29
  • 65