Home Templates and method specialization
Reply: 0

Templates and method specialization

user2358 Published in March 22, 2018, 9:16 pm

I am learning on my own about templates and i am having a pretty rough time figuring some things out. specially when it comes to partially specializing a method or a function.

I am building a small library for doing some math with tensors(mostly as a learning experience) and i figured that using templates for my Tensor data structure would be the best call, so i started learning about them and their usage; this is what i (with some help) came up with:

template<typename T, std::size_t...>
    class Tensor;

template<typename T, std::size_t N>
    class Tensor<T, N>{
        T data[N];

template<typename T, std::size_t N, std::size_t M>
    class Tensor<T, N, M>{
        using dataType = Tensor<T, M>;
        dataType data[N];

template<typename T, std::size_t N, std::size_t... Ms>
    class Tensor<T, N, Ms...>{
        using dataType = Tensor<T, Ms...>;
        dataType data[N];

now i want to add an operator to Tensor<T,N,M> that can be used to operate with other templated classes. And so, i add this to my class definition(i expect P to be deduced from the arguments that are passed to the method):

template<std::size_t P>
    Tensor<T,N,P> operator*(Tensor<T,M,P> other);

and when i want to implement it i do it like this:

template<typename T, std::size_t N, std::size_t M, std::size_t P>
    Tensor<T,N,P> Tensor<T,N,M>::operator*(Tensor<T,M,P> other){
        Tensor<T,N,P> result;
        // do other stuff
        return result;

this throws the following compile time errors:

invalid use of incomplete type 'class MesTen::Tensor<T, N, M>' --- this one at the line where the implementation of operator* starts

error: declaration of 'class MesTen::Tensor<T, N, M>' --- and this one at the line where i declare the empty Tensor class

How is my syntax wrong? or is there something deeper that i didn't understand about templates and how they work?

If a solution is provided, having a function instead of a method or an operator is acceptable but definitely not prefered.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.295326 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO