#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]; }
这个运算符是什么东西?它是一个函数吗?如果是的话为什么它后面有方括号和“const”? 这些模板还有什么意义吗?我假设它执行其中之一 取决于 t 的值( true 或 false )'
最佳答案
What is this operator thing? Is it a function? If it is then why does it have square brackets
您正在声明 operator[]
作为名为 line
的类模板的成员函数。通过提供此信息,我们说我们正在重载 operator[]
对于我们的类模板line
(实际上是针对将被实例化的特定类类型)。
why does it have
const
after it
const
意味着这个 operator[]
成员函数是一个const 成员函数。这意味着我们不允许更改此成员函数内的非静态非可变数据成员。
Also do these template things mean?
假设您询问的是 template<>
正如问题标题所示,这意味着您显式(完全)特化成员函数 operator[]
对于不同的类模板参数 0
和1
.
更多详细信息可以在任何 good C++ books 中找到。 .
关于c++ - "template <> int line<0>::operator[](int y) const"有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72379364/