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?