lundi 16 décembre 2019

Issue with Lambda's capture clause

I am trying to use ftw function to calculate size of a directory. The prototype of ftw function is:

int ftw(const char *dirpath,
        int (*fn) (const char *fpath, const struct stat *sb,
                   int typeflag),
        int nopenfd);

To calculate the size of the entire directory, I am trying to use below code using lambda expression:

uint32_t calcDirSize(const char *path) {
   uint32_t usize = 0;

   if (ftw(path, [&usize](const char *fpath, const struct stat *sb, int typeflag) {
                     usize += sb->st_size;
                     return 0;
             }, 1)) {
    return 0;
   }

   return usize;
}

It is throwing error for the variable in the capture clause of the lambda expression. I want to use a local variable to calculate the size and return from calcDirSize function once it is calculated. Is there any other way to achieve the same result?

Aucun commentaire:

Enregistrer un commentaire