0

I wanted to test which method is faster to clean up a std::deque using std::deque::cleaner, std::deque::swap and std::deque::pop_back.

To do that I created a program that has:

  1. a function to test Testit and receive a lambda with the specific method;
  2. a struct with the description of the method, a lambda with the method itself and the result;

When I run the program I've been got "bad_function_call".

What is wrong with the program and how can I fix it to make the struct/class works as expected with lambdas members?

Below you can see the important part of the program:

//Procedure to test. It receive a lambda with the method to clean the deque up.
int Testit(auto& fCleanUp_the_Container) {
    (...)
    deque<string> vdTEST;              //deque to be tested
    //Add elements to vdTEST

    fCleanUp_the_Container(vdTEST);
    (...)
}

struct dData {
    //The Description and lMethodtoCleanUp will be initialize with the std::vector::emplace_back method.
    dData(string Description, function<void(deque<string>& vdTEST)> lMethodtoCleanUp) {};
    int Results = 0;
    string Description;
    function<void(deque<string>& vdTEST)> lMethodtoCleanUp;
};

int main () {
    (...)
    //upload the data to a vector;
    vector<dData> dDt;
    dDt.emplace_back("clear", [](deque<string>& vdTEST) {vdTEST.clear();}); //Using std::deque::clear
    dDt.emplace_back("swap", [](deque<string>& vdTEST){deque<string> vdTMP; vdTEST.swap(vdTMP);}); //Using std::deque::swap
    dDt.emplace_back("pop_back", [](deque<string>& vdTEST){while (!vdTEST.empty()) vdTEST.pop_back();}); //Using std::deque::pop_back

    //running the test
    for (int iIta=1; iIta != noofCycles; iIta++) {
        cout << "\nCycle " << iIta << "...";
        for (auto& iIt : dDt)                           //Test all members of dDt
            iIt.Results += Testit(iIt.lMethodtoCleanUp);
    }

    (...)
 }
TheArchitect
  • 1,160
  • 4
  • 15
  • 26
  • 2
    You don't initialise `lMethodtoCleanUp`, how do you plan to call it without crashing your program? – n. m. could be an AI Jun 13 '17 at 06:44
  • 2
    Do what @n.m. suggested. Take a close look at the constructor of `dData`. What exactly is it initializing? – StoryTeller - Unslander Monica Jun 13 '17 at 06:59
  • bad_function_call is the error you get when you try to use uninitialized std::function or the one which value has been moved. dData constructor doesn't initialize any of the structure fields. The most simple way to correct it is to add initialization list to the constructor: dData(...) : Description(std::move(Description)), lMethodtoCleanUp(std::move(lMethodtoCleanUp)) {}; – Dmitry Gordon Jun 13 '17 at 07:11
  • 1
    *It will receive the description and the lambda* yes, but what will it **do** with them? – n. m. could be an AI Jun 13 '17 at 07:23
  • The description will be shown together with the Results in the end (it is not showed on example because it is not part of problem). The lambda is the method used by function 'testit'. – TheArchitect Jun 13 '17 at 07:27
  • That's what the class is supposed to do with its members. I asked what the constructor does with its parameters (not the same thing). – n. m. could be an AI Jun 13 '17 at 07:49

1 Answers1

2

Your constructor

dData(string Description, function<void(deque<string>& vdTEST)> lMethodtoCleanUp) {};

ignores its parameters Description and lMethodtoCleanUp. It does not initialise class members which also happen to be named Description and lMethodtoCleanUp.

You should do two things:

  1. Read up on members initialisation lists (here is one relevant SO question among many others).
  2. Turn on all compiler warnings and treat them as errors.

A correct constructor implementation would be:

dData(string Description, function<void(deque<string>& vdTEST)> lMethodtoCleanUp) 
   : Description(Description), lMethodtoCleanUp(lMethodtoCleanUp) {};

Note in e.g. Description(Description) the two occurrences of Description refer to two different variables: the class member and the constructor parameter.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243