Блог пользователя LeoPro

Автор LeoPro, история, 21 месяц назад, По-русски

Hello!

I have yet another C++ question I want to know answer to. That is:

  • This code doesn't compile with error message use of deleted function ‘main()::<lambda(auto:1)>::~<lambda>()’
int main() { // CE
    int n = 1;
    int a[n];
    auto f = [&](auto f) -> void {
        a[0] = 0;
    };
}
  • On the other side, replacing C-array with vector or specifying array length as compile-time constant or allocating it with new — every other way makes everything to work:
int main() { // OK
    int n = 1;
    vector<int> x(n);
    int y[1];
    int *z = new int[n];
    auto f = [&](auto f) -> void {
        x[0] = y[0] = z[0] = 0;
    };
}
  • The error also goes off if the lambda f doesn't accept any arguments declared as auto. It also disappears if I capture the array by link directly: auto f = [&, &a].

So, why is it the case? I managed to find out that variable sized array are GCC extension but it doesn't clarify the doubt. The problem is not in the array allocation / initialization, but in lambda's destructor, which clearly needn't to destruct the array.

As far as I know, the array is just a pointer to its beginning and capturing it by copy, or by reference makes small difference — you either create new variable that points to the beginning of the array too, or you create a reference to the "main" pointer. Doesn't seem to be a problem.

So, this is quite a peculiar problem; it arose when I tried to combine recursive lambdas and C-arrays. Probably some C++ master would share some thoughts and improve my understanding and intuition of the language.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +10
  • Проголосовать: не нравится

Автор LeoPro, история, 3 года назад, По-английски

Hello!

Recently I've faced a confusing problem:

I've accidentally re-submitted my solution for problem 1554E - You on different compiler. Suddenly I've noticed that its running time has significantly increased.

  • This solution 128134170 takes 1263 ms under GNU C++17 compiler.
  • This solution 128134185 takes 2277 ms under GNU C++17 (64) compiler.
  • You can see that their codes are fully identical.

So, common code with some vectors, self-written modular class and lambdas causes that much time difference — approximately 2 times. The memory also increased (1.5x times), it is also kinda suspicious because I haven't used any pointers.

I've re-written it without lambdas — lifted dfs out of solve (128134438 and 128134426), both execution times have increased in about 200 ms.

I had somewhere heard that vector<bool> is very problematic structure. I even have changed it to vector<int> and resubmitted again (128141354 and 128141276). The execution times have increased a lot and gone closer, but the gap is still quite big (2105 ms / 2807 ms).

So, I am quite at loss, why would 64-bit compiled program take (2 times) more time to execute. Can anyone help me and explain the reason?

P. S. I can hardly think of any good query for googling. This comment is talking about something similiar; however, it isn't answered.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +111
  • Проголосовать: не нравится