vendredi 23 juin 2017

Understanding this->templates and typename

I am new to templates and was trying to understand the below code. In all the tutorials I have read, there is no reference to typename... .

So my first question is what is typename... and also what is the signifigance of ... almost eveywhere in the code?

What does this->template do? As far as I have read, templates are not objects ( they dont exist at run time ) , so what is the this pointer referring to?

Does the template line above the function subplot() makes sense? Because the template is normally used, so that the compiler can replace the function ( subplot ) differently according to the template parameters. But if the template parameters are fixed, then whats the use of template at all?

    template<uint32_t _num, typename... _types>
    class _GridLayoutInputRepeater : public _GridLayoutInputRepeater<_num-1,_types...,Figure> {
    private:
        static const int _index = sizeof...(_types);
        typedef _GridLayoutInputRepeater<_num-1,Figure,_types...> _base;

    public:
        _GridLayoutInputRepeater() {
            this->template input<_index>()->checkPortSize(false);
        }

        template<uint32_t _width, uint32_t _height, uint32_t _cols, uint32_t _rows>
        void subplot(Figure* out) {
            const uint32_t _row = _index / _cols;
            const uint32_t _col = _index - _row * _cols;
            cv::Rect rect;
            cv::Mat3b subfig, tmp;
            Figure* in;

            rect = cv::Rect(_col*_width,_row*_height,_width,_height);
            subfig = out->mat();
            subfig = subfig(rect);
            in = this->template input<_index>()->value();

            if(in->mat().channels() == 1)
                cv::cvtColor(in->mat(),tmp,cv::COLOR_GRAY2BGR);
            else
                tmp = in->mat();

            cv::resize(tmp,subfig,subfig.size());

            this->_base::template subplot<_width,_height,_cols,_rows>(out);
        }
    };

Aucun commentaire:

Enregistrer un commentaire