template<>
struct params {
typedef uint16_t value_type;
typedef int16_t signed_value_type;
typedef uint32_t greater_value_type;
typedef value_type* poly_t;
// The moduli used in each 16 bit block (14 bits long each)
// They are of the form p = 2*14 - i2*kMaxPolyDegree + 1 for increasing i
static constexpr unsigned int kMaxNbModuli = 2;
static constexpr value_type P[kMaxNbModuli] = { 15361U, 13313U};//有问题
// The associated lower word of their Newton quotients
static constexpr value_type Pn[kMaxNbModuli] = { 17458U, 60470U};//有问题
static constexpr unsigned int kModulusBitsize = 14;
static constexpr unsigned int kModulusRepresentationBitsize = 16;
// A primitive 2*kMaxNbModuli (i.e., 2**10) root of unity for each one of the moduli
static constexpr value_type primitive_roots[kMaxNbModuli] = { 4989U, 10076U};//有问题
// Inverses of kMaxPolyDegree (for the other degrees it can be derived easily)
// for the different moduli
static constexpr value_type invkMaxPolyDegree[kMaxNbModuli] = { 15331U, 13287U};//有问题
// Polynomial related data
static constexpr unsigned int kMaxPolyDegree = 512;
};
vs2015编译时遇到问题:
(代码中标注//有问题的语句都是下面形式的error,我把代码改成 static constexpr int P[2] = { 1,1};也是这样的error)
error C4579: 'nfl::params::P': in-class initialization for type 'const nfl::params::value_type [2]' is not yet implemented; static member will remain uninitialized at runtime but use in constant-expressions is supported
error C2131: 表达式的计算结果不是常数
note: 遇到非常量(子)表达式
static 常量成员,除完全用于类内初始化的整型(kMaxNbModuli ),其余必须类内声明初始化,类外定义。
#include <cstdint>
struct params
{
using value_type = uint16_t;
using signed_value_type = int16_t;
using greater_value_type = uint32_t;
using poly_t = value_type *;
// The moduli used in each 16 bit block (14 bits long each)
// They are of the form p = 2*14 - i2*kMaxPolyDegree + 1 for increasing i
static constexpr unsigned int kMaxNbModuli = 2;
static constexpr value_type P[kMaxNbModuli] = {15361U, 13313U};
// The associated lower word of their Newton quotients
static constexpr value_type Pn[kMaxNbModuli] = {17458U, 60470U};
;
static constexpr unsigned int kModulusBitsize = 14;
static constexpr unsigned int kModulusRepresentationBitsize = 16;
// A primitive 2*kMaxNbModuli (i.e., 2**10) root of unity for each one of
// the moduli
static constexpr value_type primitive_roots[kMaxNbModuli] = {4989U, 10076U};
// Inverses of kMaxPolyDegree (for the other degrees it can be derived
// easily) for the different moduli
static constexpr value_type invkMaxPolyDegree[kMaxNbModuli] = {15331U,
13287U};
// Polynomial related data
static constexpr unsigned int kMaxPolyDegree = 512;
};
constexpr params::value_type params::P[kMaxNbModuli];
constexpr params::value_type params::Pn[kMaxNbModuli];
constexpr params::value_type params::primitive_roots[kMaxNbModuli];
constexpr params::value_type params::invkMaxPolyDegree[kMaxNbModuli];
int main()
{
params a;
return 0;
}
这个是不是要把定义和声明分开写啊
是不是结构里不允许这样子初始化数组啊
如果我把static constexpr value_type P[kMaxNbModuli] = { 15361U, 13313U};这里改成value_type P[kMaxNbModuli] = { 15361U, 13313U};是可以的,但是另一段代码就会出现另外的问题:
namespace nfl {
constexpr typename params::value_type params::P[];//error C2761: nfl::params::value_type nfl::params::P[2]”: 不允许成员函数重新声明
constexpr typename params::value_type params::Pn[];
constexpr typename params::value_type params::primitive_roots[];
constexpr typename params::value_type params::invkMaxPolyDegree[];
}
你肯定是别的地方有问题,建议你查看一下params 泛化的声明
如果要使用模板,最好系统的学习一下它,毕竟它是c++比较复杂的部分