这个模板<int ...>的想法在C++中可行吗?
Is this template<int ...> idea in C++ possible?
我正在尝试学习用 C++ 实现 template
。当我将 NTT(数论变换)代码更改为使用 template
的代码时,它看起来像这样:
template <long long mod> struct NTT{
int x = 0;
NTT(){
long long p = mod-1;
while(!(p % 2)){
p >>= 1;
++x;
}
}
const static long long root_pw = 1 << x;
//(there is a function after this that also needs to read the value 'root_pw')
};
signed main()
{
NTT<998244353> ntt1;
vector<long long> a(ntt1::root_pw,0);
}
它告诉我制作 x static
。
当我这样做时,它告诉我制作 x const
,这打败了 x 最初存在的原因。
我使用 (GNU C++11) 和我的编译器 (Dev-C++ 5.11) 设置为配置 (TDM-GCC 4.9.2 64-bit Release),如果有帮助的话。
我真的很想做这个,但我不知道怎么做。
这可能非常简单,但我错过了什么?
提前致谢。
你可以替换C++14函数
template <long long mod>
constexpr int f()
{
int x = 0;
long long p = mod-1;
while(!(p % 2)){
p >>= 1;
++x;
}
return x;
}
C++11 版本:
template <long long p>
constexpr int f2()
{
return p % 2 ? 0 : 1 + f2<p / 2>();
}
template <long long mod>
constexpr int f()
{
return f2<mod - 1>();
}
等等
template <long long mod>
struct NTT{
constexpr static const long long root_pw = 1LL << f<mod>();
};
我正在尝试学习用 C++ 实现 template
。当我将 NTT(数论变换)代码更改为使用 template
的代码时,它看起来像这样:
template <long long mod> struct NTT{
int x = 0;
NTT(){
long long p = mod-1;
while(!(p % 2)){
p >>= 1;
++x;
}
}
const static long long root_pw = 1 << x;
//(there is a function after this that also needs to read the value 'root_pw')
};
signed main()
{
NTT<998244353> ntt1;
vector<long long> a(ntt1::root_pw,0);
}
它告诉我制作 x static
。
当我这样做时,它告诉我制作 x const
,这打败了 x 最初存在的原因。
我使用 (GNU C++11) 和我的编译器 (Dev-C++ 5.11) 设置为配置 (TDM-GCC 4.9.2 64-bit Release),如果有帮助的话。
我真的很想做这个,但我不知道怎么做。
这可能非常简单,但我错过了什么?
提前致谢。
你可以替换C++14函数
template <long long mod>
constexpr int f()
{
int x = 0;
long long p = mod-1;
while(!(p % 2)){
p >>= 1;
++x;
}
return x;
}
C++11 版本:
template <long long p>
constexpr int f2()
{
return p % 2 ? 0 : 1 + f2<p / 2>();
}
template <long long mod>
constexpr int f()
{
return f2<mod - 1>();
}
等等
template <long long mod>
struct NTT{
constexpr static const long long root_pw = 1LL << f<mod>();
};