Home > other >  What does "template <> int line<0>::operator[](int y) const" do?
What does "template <> int line<0>::operator[](int y) const" do?

Time:05-26

#include <bits/stdc  .h>
using namespace std;

constexpr int mod = 1e9   7, maxn = 2e6;
int N, M, p[1 << 10], buf[maxn];

template <bool t> struct line {
    int *v;
    int operator[](int y) const;
};       

template <> int line<0>::operator[](int y) const { return v[y]; }
template <> int line<1>::operator[](int y) const { return v[M * y]; }



What is this operator thing? Is it a function? If it is then why does it have square brackets and "const" after it? Also do these template things mean? I'm assuming that it executes one of them depending on the value of t ( true or false )'

CodePudding user response:

What is this operator thing? Is it a function? If it is then why does it have square brackets

You're declaring operator[] as a member function of the class template named line. By providing this, we say that we're overloading operator[] for our class template line(really for a specific class type that will be instantiated).


why does it have const after it

The const means that this operator[] member function is a const member function. Meaning we're not allowed to change the non-static non-mutable data members inside this member function.


Also do these template things mean?

Assuming you are asking about the template<> as the question title suggests, it means that you're explicitly(fully)specializing the member function operator[] for different class-template arguments 0 and 1.


More details can be found in any of the good C books.

Also refer to Why should I not #include <bits/stdc .h>?.

  • Related