Iterator with Static Array Member variable

throw_away_account

Is it a bad idea to define a static array member variable for an iterator?

In the following nested loop, will the static array be created and destroyed for 1000 times? (suppose we use compiler optimization)

X is a range object with begin() and end() member functions. The two member functions both return an iterator with a static array member variable.

for (int i = 0; i < 1000; ++i) {
    for (auto i: X) {
        // do stuff.
    }
}
lorro

I assume you want to copy something into the static array on iterator construction on each loop. I further assume that you overwrite the entire array and you're doing this because you've realized that data would get lost otherwise. I'd be happy to throw away that assumption if you use it for sg. else, but that's a very common issue with range for. Your code won't be reentrant, let alone thread-safe. That means that, depending on how you do this, you might not even be able to do nested loops. That's a very bad idea... ... yuck, just about as bad as you get with range for vs. temporaries. Perhaps the best thing/workaround to do is keeping a shared_ptr<> to what you want to keep. This might include the range itself if you write this owning begin() and end() for the ptr, not the range. (Either this obscurity or potential UB from enable_shared_from_this()).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Pointer to member variable as static member

From Java

Why does this static const int member variable appear to be accessible publicly in array definition?

From Dev

Initialize static atomic member variable

From Dev

static const member variable initialization

From Dev

Initialize static member array to zero

From Dev

static member variable in class template

From Dev

Is each static array member initialized?

From Dev

C++ static variable in member function

From Dev

Android memory leak on static Resource member variable?

From Dev

Binding member function to a local static variable

From Dev

constexpr static member vs variable

From Dev

Strange behavior with constexpr static member variable

From Dev

Static class member variable in static library not shared?

From Dev

Declaring a static private array member

From Dev

Static member function to initialize static member variable - usage and risks?

From Dev

Create Shared Member Variable between instances (static)

From Dev

Assign static constexpr class member to runtime variable

From Dev

PHP static method call with namespace in member variable

From Dev

Type trait: Check if reference member variable is static or not

From Dev

Cannot access static member variable from class in array

From Dev

Ambiguity in a fully qualified static member variable

From Dev

Static member variable such as OOP langage

From Dev

c++11 static functional member variable

From Dev

Initialize static atomic member variable

From Dev

Instantiation of template static member variable

From Dev

Static array variable in javascript?

From Dev

Static class member variable and static variable in c++

From Dev

static member variable initializing twice

From Dev

How to refering a static variable in a class static member?

Related Related

  1. 1

    Pointer to member variable as static member

  2. 2

    Why does this static const int member variable appear to be accessible publicly in array definition?

  3. 3

    Initialize static atomic member variable

  4. 4

    static const member variable initialization

  5. 5

    Initialize static member array to zero

  6. 6

    static member variable in class template

  7. 7

    Is each static array member initialized?

  8. 8

    C++ static variable in member function

  9. 9

    Android memory leak on static Resource member variable?

  10. 10

    Binding member function to a local static variable

  11. 11

    constexpr static member vs variable

  12. 12

    Strange behavior with constexpr static member variable

  13. 13

    Static class member variable in static library not shared?

  14. 14

    Declaring a static private array member

  15. 15

    Static member function to initialize static member variable - usage and risks?

  16. 16

    Create Shared Member Variable between instances (static)

  17. 17

    Assign static constexpr class member to runtime variable

  18. 18

    PHP static method call with namespace in member variable

  19. 19

    Type trait: Check if reference member variable is static or not

  20. 20

    Cannot access static member variable from class in array

  21. 21

    Ambiguity in a fully qualified static member variable

  22. 22

    Static member variable such as OOP langage

  23. 23

    c++11 static functional member variable

  24. 24

    Initialize static atomic member variable

  25. 25

    Instantiation of template static member variable

  26. 26

    Static array variable in javascript?

  27. 27

    Static class member variable and static variable in c++

  28. 28

    static member variable initializing twice

  29. 29

    How to refering a static variable in a class static member?

HotTag

Archive