Questions tagged [std-function]

A C++11 class template that is callable like a function, and wraps another callable type and forwards calls to it.

In the words of the C++11 standard std::function is a polymorphic wrapper class that encapsulates arbitrary callable objects. It is polymorphic because an instance of the type std::function<R (A1, A2)> could wrap an object of many different callable types, including:

  • a function pointer of type R (*)(A1, A2)
  • a function pointer of type R (*)(const A1&, const A2&)
  • a function object (a.k.a functor) with a member function such as R C::operator()(A1, A2)
  • a pointer to member function of type R (A1::*)(A2)
  • a lambda [](A1 a1, A2 a2) -> R {...}

std::function is often used to implement generic callbacks or to support passing arbitrary callable types to a function that cannot be written as function template e.g. because it must be virtual.

Use this tag for questions about std::function and std::tr1::function.

844 questions
5
votes
1 answer

Is using shared_ptr and weak_ptr to manage lifetime of std::function safe?

I've created a wrapper around boost::asio::io_service to handle asynchronous tasks on the GUI thread of an OpenGL application. Tasks might be created from other threads so boost::asio seems ideal for this purpose and means I don't need to write my…
RandomEtc
  • 1,976
  • 1
  • 18
  • 17
5
votes
2 answers

C++ template class error: function returning a function

I want to make a simple logger which automatically runs a function and returns its value. The class is defined as: template class Logger3 { Logger3(function func, const string& name): …
5
votes
1 answer

Why is type checking not happening for std::function?

#include void toggleOk(bool& b) { b = !b; } void toggleBroken(bool b) { b = !b; } void toggleInt(int i) { i = !i; } void tooManyParams(bool b, int i) { i = !b; } int main() { typedef std::function CallbackType; …
doomista
  • 501
  • 2
  • 10
5
votes
1 answer

C++: conversion from overloaded function to std::function

I have a C++ function that takes in two other functions as parameters: // ParseResult is a class with a default constructor (no arguments) ParseResult* bin_op(std::function func_a, std::vector ops,…
Diogenis Siganos
  • 779
  • 7
  • 17
5
votes
1 answer

non-capturing lambda and function pointer as parameter in overloaded function ambiguity

#include #include template void test( std::function< void ( const T& ) > f ) { T val {}; f( val ); std::cout << "std::function" << std::endl; } template void test( void(*f) ( const T& )…
calynr
  • 1,264
  • 1
  • 11
  • 23
5
votes
1 answer

Does the C++ standard explicitly allow/disallow instantiating std::function with incomplete types?

Consider something like this: class A; std::function f; This is clearly something you want to do in your program. Any major compiler accepts that without problems. My question is: is it technically allowed by the standard? If I see…
FilippoL
  • 160
  • 7
5
votes
3 answers

How to pass an overloaded member-function as parameter?

Here is the problem I am facing: I have an overloaded function in a class, and I want to pass one of its overloads as a parameter. But when doing so, I get the following error : "no suitable constructor exists to convert from to…
Midnight Exigent
  • 615
  • 4
  • 15
5
votes
3 answers

How can I pass a method of an object's class as std::function?

I have a function that needs to get std::function-type parameter. I also have an abstract-type pointer to an object. Is it possible to pass the method using only an object? The function signature: void foo(std::function); The classes…
lemonade
  • 87
  • 1
  • 5
5
votes
2 answers

Templated function with defaulted std::function parameter causes symbol [...] already defined

I just ran into something somewhat odd. I wonder if it's a bug, and if not I hope someone can explain the issue. My issue is that when I create a templated function which takes an std::function as a defaulted parameter, I can only create one…
jpihl
  • 7,941
  • 3
  • 37
  • 50
5
votes
1 answer

Tagging std::function with a name?

I'm working on a parser combinator library, and I'd really like my parser to simply be some callable object: typedef std::function parser; Which makes the parser combinators nice, eg: parser operator &(parser a, parser…
gct
  • 14,100
  • 15
  • 68
  • 107
5
votes
2 answers

is std::function heavier than auto storing lambda function

I heard that cost of std::function is heavier than auto to deal with a lambda function. effective modern c++ item5. What I want is to clarify the mechanism why std::function use more memory than auto with some sample code. Could somebody help…
Daniel Lee
  • 205
  • 3
  • 7
5
votes
1 answer

Low latency callback in C++

I have an event driven application. I want to keep the event handler (EventHandler class capable of many/all events) a common implementation - while allowing the EventSource be changeable (specifically - at compile time). To couple the EventHandler…
rat6
  • 81
  • 9
5
votes
2 answers

how to pass variadic params to virtual function

I have a working virtual function add which uses following setup: using Func = std::function()>; class cfExecutor { public: cfExecutor(); virtual ~cfExecutor(); /// Enqueue a function to be executed by…
serup
  • 3,676
  • 2
  • 30
  • 34
5
votes
4 answers

Using an std::function for wrapping a function object

Can someone help me to understand why the following code causes an error? class A { public: float& operator()() { return _f; } private: float _f = 1; } a; auto& foo() { std::function func = a; return…
abraham_hilbert
  • 2,221
  • 1
  • 13
  • 30
5
votes
2 answers

std::functions and lambda function passing

I have a class that takes a std::function as a parameter which I assign a lambda function. It works in the constructor but it stops working after that. The debugger says f is "empty" after running the first line. Why? #include #include…
Mochan
  • 1,329
  • 1
  • 13
  • 27