Search code examples
c++templatestemplate-argument-deductiontemplate-templates

template template function definition


I have a template class called Array

template<typename T, int dimension>
class Array<typename T, int dimension>{

    //definition of the class

}

I want to write a non-member function cast such that I can cast Array into different type. For example

Array<int, 2> a;
Array<float, 2> b = cast<float>(a);

How should I write this function? I am more interested in how to declare it instead of how to implement the detailed casting. I have tried

template<template<typename T, int dimension> class Array, typename New_T, int dimension>
Array<typename New_T, int dimension> cast(Array<typename T, int dimension> a){

// detailed implementation of casting, which I do not care for this question.

}

but it cannot pass the compilation.


Solution

  • How should I write this function? I am more interested in how to define it instead of how to implement the detailed casting.

    I suppose something like

    template <typename ToT, typename FromT, int Dim>
    Array<ToT, Dim> cast (Array<FromT, Dim> const & inA)
     {
       // ...
     }
    

    It's useful place ToT (to-type) in first position so you can explicit it and let FromT and Dim deduced from the inA value.

    --- EDIT ---

    The OP asks

    Any insight why I have to put it [ToT] in the first position?

    You don't necessarily have to put ToT in first position. But this simplify your life.

    The point is that FromT and Dim are deducible from the inA argument; ToT isn't deducible from arguments so you have to explicit it.

    But if you want to explicit a template parameter, you necessarily have to explicit the preceding parameters. So if you put ToT in last position, you have to call cast() explicating all template parameters

    cast<int, 2, float>(a);
    

    If you place ToT in first position, you have to explicit only it and leave the compiler deduce FromT and Dim from the argument

    cast<float>(a);