Thanks.
When I change to
auto a=layers[0+i]->as()->forward(x);
The error reports “index  out of range”
terminate called after throwing an instance of ‘c10::Error’
I defined the layers as
struct DiscriminatorImpl:torch::nn::Module {
torch::Tensor x;
torch::nn::ModuleList layers;
LinearWeightNorm final=nullptr;

int64_t input_dim=28*28;
Scalar output_dim=10;
Scalar weight_scale=1;
DiscriminatorImpl(){}
 DiscriminatorImpl(int64_t input_dim, int64_t output_dim)
{
    layers = register_module("layer", torch::nn::ModuleList(
        
            LinearWeightNorm(input_dim, 1000, true, 1, 0.1),
            LinearWeightNorm(1000, 500,true, 1, 0.1),
            LinearWeightNorm(500, 250, true, 1, 0.1),
            LinearWeightNorm(250, 250, true, 1, 0.1),
            LinearWeightNorm(250, 250, true, 1, 0.1)
    ));
    final= register_module("final",LinearWeightNorm(250, output_dim, true,1, 0.1));
}

May I ask how shall I initialize the ModuleList properly in this case please ?
Thanks.