4

I want to use either push_front or push_back depending on user input. Is it possible to have that included in the function parameter, then write one function in order to save on memory?

JeJo
  • 30,635
  • 6
  • 49
  • 88
hyperonet
  • 41
  • 2
  • 1
    Welcome to SO! Search "C++ function pointer" for answers to your question, like https://stackoverflow.com/questions/1952175/how-to-call-the-function-using-function-pointer – Dave S May 12 '19 at 06:26
  • 3
    @kmdreko No, [you can't](https://stackoverflow.com/a/55687045). – L. F. May 12 '19 at 06:39
  • 1
    @L.F. whaaat? I didn't know that... that's lame, thanks for the link though – kmdreko May 12 '19 at 06:51
  • 1
    @JeJo There is a mention of member functions at the very end of the answer. – L. F. May 12 '19 at 06:52

2 Answers2

3

Is it possible to have that included in the function parameter?

No. You can't.

A good explanation can be found in this discussion: Can I take the address of a function defined in standard library?


However, a workaround can be done by telling the function explicitly where the insertion should take place.

#include <iostream>
#include <deque>

enum class To{ front = 0, back };

void func(std::deque<int>& dq, const int value, const To where)
{
    if (where == To::front)
    {
        dq.push_front(value);
        // code
    }
    else if(where == To::back)
    {
        dq.push_back(value);
        // code
    }
}

And you call it

std::deque<int> dq;
func(dq, 1, To::back);
func(dq, 2, To::front);
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • Instead of the variable name `where`, more intuitive would be using `position`, as it refers to the beginning or end of the std::deque. – UserUsing May 12 '19 at 21:17
1

It is possible using lambdas:

#include <deque>

void f(bool user_input) {
    using C = std::deque<int>;
    using FnType = void(*)(C&, int);
    FnType const fns[2] = {
        [](C& c, int value) { c.push_front(value); },
        [](C& c, int value) { c.push_back(value); }
    };
    C q;
    fns[user_input](q, 1);
    fns[!user_input](q, 2);
}

In this examples, the closures created by lambda expressions have 0 captures and hence can be converted to plain function pointers. When there are more than 0 captures std::function can be used instead of plain function pointers.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • Upped for the lambdas. However, isn't it a bit little inconvenient for the user to remember which lambda function of the array is for **push_back** and **push_front**? – UserUsing May 12 '19 at 21:15
  • @UsingCpp The array's purpose here is to map boolean user input to either of these functions. One can use another type of user input and a different mapping. – Maxim Egorushkin May 12 '19 at 22:29