0

I have the following code:

#include <array>
#include <iostream>
#include <typeinfo>
#include <type_traits>
#include <utility>

namespace impl
{
    template <typename T>
    struct Matrix_traits {

    };
}

template <size_t M, size_t N, typename T>
class Matrix
{

};

template <size_t M, size_t N, typename T>
struct impl::Matrix_traits<Matrix<M, N, T>> {
    template <typename U>
    struct scalar_mult_type
    {
        // just for testing
        using type = std::pair<std::array<T, M>, std::array<T, N>>;
    };
};

int main()
{
    Matrix<3, 4, char> m;

    mult(3, m);

    return 0;
}

When I use the following function implementation, where I'm explicitly specifying the return type:

template <typename T, typename U>
std::pair<std::array<char, 3>, std::array<char, 4>> mult(const T& lambda, const U& m)
{
    typename impl::Matrix_traits<U>::scalar_mult_type<T>::type result;
    std::cout << typeid(result).name() << "\tEUREKA!\n";
    return result;
}

It works... but this is obviously not what I want... but when I'm trying to be more flexible:

template <typename T, typename U>
typename impl::Matrix_traits<U>::scalar_mult_type<T>::type mult(const T& lambda, const U& m)
{
    typename impl::Matrix_traits<U>::scalar_mult_type<T>::type result;
    std::cout << typeid(result).name() << "\tEUREKA!\n";
    return result;
}

I have "unrecognizable template declaration/definition" error. It's a real puzzle to me. Why the same declaration works for local variable 'result' but fails as the return type?

Mirek
  • 13
  • 1
  • What is that `struct impl::Matrix_traits>` supposed to mean? – SergeyA Nov 02 '15 at 16:39
  • Your `Matrix_Traits` take one template parameter in one case and 3 in the other case. Could you try to explain what you want to use the traits for? – Simon Kraemer Nov 02 '15 at 16:51
  • @SimonKraemer: the second case is a (partial) specialization. – Jarod42 Nov 02 '15 at 17:07
  • see [where-and-why-do-i-have-to-put-the-template-and-typename-keywords](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – Jarod42 Nov 02 '15 at 17:14

1 Answers1

0

template are missing, it should be

template <typename T, typename U>
typename impl::Matrix_traits<U>::template scalar_mult_type<T>::type
mult(const T& , const U& )
{
    typename impl::Matrix_traits<U>::template scalar_mult_type<T>::type result;
    std::cout << typeid(result).name() << "\tEUREKA!\n";
    return result;
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • @Mirek: The way to thank is to accept/upvote answers. Comment is mostly to ask or add complement. – Jarod42 Nov 03 '15 at 16:13