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?
Is it possible to pass std::deque's member functions as a parameter?
1.2k Views Asked by hyperonet At
2
There are 2 best solutions below
2
Maxim Egorushkin
On
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.
Related Questions in C++
- How to immediately apply DISPLAYCONFIG_SCALING display scaling mode with SetDisplayConfig and DISPLAYCONFIG_PATH_TARGET_INFO
- Why can't I use templates members in its specialization?
- How to fix "Access violation executing location" when using GLFW and GLAD
- Dynamic array of structures in C++/ cannot fill a dynamic array of doubles in structure from dynamic array of structures
- How do I apply the interface concept with the base-class in design?
- File refuses to compile std::erase() even if using -std=g++23
- How can I do a successful map when the number of elements to be mapped is not consistent in Thrust C++
- Can std::bit_cast be applied to an empty object?
- Unexpected inter-thread happens-before relationships from relaxed memory ordering
- How i can move element of dynamic vector in argument of function push_back for dynamic vector
- Brick Breaker Ball Bounce
- Thread-safe lock-free min where both operands can change c++
- Watchdog Timer Reset on ESP32 using Webservers
- How to solve compiler error: no matching function for call to 'dmhFS::dmhFS()' in my case?
- Conda CMAKE CXX Compiler error while compiling Pytorch
Related Questions in FUNCTION
- Dynamic array of structures in C++/ cannot fill a dynamic array of doubles in structure from dynamic array of structures
- Function is returning undefined but should be returning a matched object from array in JavaScript
- How do you import functions from one page to another in Jetpack Compose?
- Adding Modules to a Namespace using IIFE
- How to convert mathematical expression to lambda function in C++?
- Custom Bash functions & custom statements - Need some advice
- Why my code is working on everything except one instance?
- Getting a function to call an equation
- Create Symbolic Function from Double Vector MATLAB
- Recursive calls to function passed as a parameter of another method via Consumer interface
- How can I replace a word in SQL but only if it is the last word in the string for a scalar-valued function?
- iterating through raster bands to perform calculation
- How to make this sensor keep taking readings once its when_in_range function has been activated?
- TypeError: indice_delete() takes 0 positional arguments but 3 were given
- How to modify HTML in WordPress core file
Related Questions in C++11
- lvalue and rvalue references
- c++ range-for loop over custom class ::begin() expects 1 argument, 0 provided
- Difference between INT_MIN , INT8_MIN , INT16_MIN. For MAX too
- I am getting segmentation failt while assigning the resourcemanager instance
- Prevent reordering of prefetch instruction in c++
- How to Use libcurl to Check HTTP/S Proxy?
- Why we use `class` when there's `struct` in C++?
- Memory Management in C++: Differences in allocating shared_ptr using new vs make_shared
- Does C++ range-based `for` make copies?
- Is the behaviour is determined when initliasing the inner class's static member variable's value equal to the outer class's static member variable?
- Question about initialization. The output must be zeros with C++11 and afterwards?
- How to replace non-standard "for each" received from Visual C++ users
- G ++ can not pass the parameters in using the C ++ 11 process library under Windows?
- Why the Variadic Constructor with std::is_constructible Fails to Handle Initializer List Initialization?
- Class Object Error 'Undefined Reference For'
Related Questions in MEMBER-FUNCTIONS
- Im looking for R function to import values from one data excel sheet to another excel sheet with each value with interval of 33 rows in new sheet
- Is a constructor, that is not a special member function, still a member function?
- Use template to select which templatized member function is called
- Member function that takes member function as parameter
- issue with enable_if a template member function of template class, on a non-type template parameter, when defining it out of line
- Allman indentation in Emacs with C++ in-class-defined member-functions
- How can you enable a class template member function only if a template argument was provided?
- the object has type qualifiers that are not compatible with the member function "Customer::displayCustomer"
- Call member function in derived class from another derived class
- Pass Class Member to Method Argument
- How the function of the class itself has a different address from its objects' functions' addresses?
- Explain implementation of function call operator in boost value_factory
- How to get return type of class method?
- How to return a member function pointer from a class method?
- Member function syntax works on msvc but rejected by gcc and clang
Related Questions in STDDEQUE
- deque::end results in assertion failure
- Is it safe to access the popped binary tree nodes which iterative traversed by std::stack
- What is underlying data structure of std::deque and how does it's iterator work?
- Why is there a significant time and memory difference between the two similar implementations?
- Is there a container similar to `std::deque` but with custom block size and better performance?
- How does libcxx's C++ __split_buffer push_front function work?
- Does std::deque release some of its unused chunks of memory?
- What is the internal container used in tbb::concurrent_bounded_queue?
- looping through a std::deque and remove entries
- Should it be possible to add to an iterator of an empty deque in C++?
- STL container to select and remove random item?
- Why is this C++ deque code 'less efficient' when I demodularize it?
- Do I need to delete/free dynamically allocated data (e.g. bytearray or std::vector) within a std::deque before I can call pop_back?
- Howto detect a fail of dynamic memory allocation when pushing to std::deque?
- Why doesn't std::deque allow specifying the bucket size?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
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.
And you call it