Thread: Need help with vectors of std::function

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    5

    Question Need help with vectors of std::function

    I'm tying to make what I guess would be described as an event system. A dynamic array of functions that get all get called when a single function is called. My problem is that the functions are not getting called and I can't figure out why. I'm not experienced with C++, I'm used to using c# and before that Java.

    Here's my code, well the relevant snippets.

    Code:
        static std::vector<std::function<void()>> * renderFunctions = new std::vector<std::function<void()>>;
    
    //
    
        std::vector<std::function<void()>>::iterator it;
        for (it = renderFunctions->begin(); it < renderFunctions->end(); it++) {
            (*it)();
        }
    
    //
    
        std::function<void(void)> x = testRender;
        Display::renderFunctions->push_back(x);
    
    //
    
    void testRender() {
        glUseProgram(shader);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glBindVertexArray(0);
    
        std::cout << "I'm not getting called, argghh!";
    }
    The iterator is getting called from a loop.
    Last edited by aaronhance; 04-05-2017 at 01:40 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Not enough info to go on - it works for me.
    Code:
    #include <iostream>
    #include <vector>
    #include <functional>
    
    typedef std::function<void()> f_t;
    
    void foo () {
      std::cout << "Yay!" << std::endl;
    }
    
    int main ( ) {
      std::vector<f_t> *renderFunctions = new std::vector<f_t>;
      f_t x = foo;
      renderFunctions->push_back(x);
      std::vector<f_t>::iterator it;
      for ( it = renderFunctions->begin() ; it != renderFunctions->end(); ++it ) {
        (*it)();
      }
    }
    
    $ g++ -std=c++11 foo.cpp
    $ ./a.out 
    Yay!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    5
    I've played around with it a bit more and it works when I add a function to renderFunction from within the same namespace, but this is obviously not what I want. I don't even know why that would effect it in this way.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Still no problem.
    Code:
    #include <iostream>
    #include <vector>
    #include <functional>
    
    typedef std::function<void()> f_t;
    
    namespace wibble {
      void foo () {
        std::cout << "Yay!" << std::endl;
      }
    }
    
    int main ( ) {
      std::vector<f_t> *renderFunctions = new std::vector<f_t>;
      f_t x = wibble::foo;
      renderFunctions->push_back(x);
      std::vector<f_t>::iterator it;
      for ( it = renderFunctions->begin() ; it != renderFunctions->end(); ++it ) {
        (*it)();
      }
    }
    You're just going to have to post a real compilable example that doesn't work for you for us to test.

    Random "it doesn't work" is a pointless 20 questions game.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Salem, I can't believe you'd write leaky code :P

    For every memory leak in C++, a Rust programmer gets their... wings? That doesn't sound right... XD

  6. #6
    Registered User
    Join Date
    Apr 2017
    Posts
    5
    I pushed my project to a git repo GitHub - aaronhance/GameEngine

    The iterator is in Display.cpp
    The list is in Display.h
    The rest is in Engine.cpp

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    		for (it = updateFunctions->begin(); it < updateFunctions->end(); it++) {
    			*it;
    Why does this look nothing like the code I posted?

    First of all, using < in the loop only works if your container supports random access. If you changed to a list say, then the loop is suddenly broken.
    Using != means you no longer care about container choice.

    Second, just saying *it does nothing.

    If I break my code the way yours is broken, I get nothing either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2017
    Posts
    5
    Quote Originally Posted by Salem View Post
    Code:
            for (it = updateFunctions->begin(); it < updateFunctions->end(); it++) {
                *it;
    Why does this look nothing like the code I posted?

    First of all, using < in the loop only works if your container supports random access. If you changed to a list say, then the loop is suddenly broken.
    Using != means you no longer care about container choice.

    Second, just saying *it does nothing.

    If I break my code the way yours is broken, I get nothing either.
    Your looking at the wrong bit, the renderFunctions iterator is in Display, anyway I fixed it by putting just using a wrapper function inside the namespace. Thanks!

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So does this fail for you?
    Code:
    $ cat foo.cpp
    #include <iostream>
    #include <vector>
    #include <functional>
    
    typedef std::function<void()> f_t;
    
    namespace Display {
      std::vector<f_t> * renderFunctions = new std::vector<f_t>;
      void render(); 
    }
    
    void Display::render() {
      std::vector<f_t>::iterator it;
      for ( it = renderFunctions->begin() ; it != renderFunctions->end(); ++it ) {
        std::cout << "boo" << std::endl;
        (*it)();
      }
    }
    
    void testRender () {
      std::cout << "Yay!" << std::endl;
    }
    
    int main ( ) {
      f_t x = testRender;
      Display::renderFunctions->push_back(x);
      Display::render();
    }
    $ g++ -std=c++11 foo.cpp
    $ ./a.out 
    boo
    Yay!
    $ 
    $ g++ --version
    g++-5.real (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
    Copyright (C) 2015 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    It's your git-repo code with all the fluff removed.
    It doesn't need your namespace wrapper hackery to make it work (for me at least).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2017
    Posts
    5
    Yeah, that code works, in a new project. It's quite odd.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Since you're using git, why not create a test branch and start chipping away at your code (removing stuff which isn't necessary to demonstrate the problem).

    When you're done, you can just delete the branch.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vectors and the resize function
    By mrJTparadise in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2012, 09:45 PM
  2. converting function to include vectors
    By a.mlw.walker in forum C Programming
    Replies: 17
    Last Post: 08-10-2011, 08:33 AM
  3. problems with stl vectors function push_back
    By _lazycat_ in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2009, 02:53 PM
  4. Vectors of pointers and function templates
    By 7words in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2004, 11:39 AM
  5. ternary operator(?), vectors, and recursive function
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2003, 03:19 PM

Tags for this Thread