Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

STL container of functions?

32 views
Skip to first unread message

eva galois

unread,
Aug 20, 2016, 11:43:50 PM8/20/16
to
Is it possible, through templates/aliasing('using')/etc, to create a
container of functions with arbitrary return value and parameters?

I know this code is basically gibberish, but the idea would be something
like:

| template <typename T, typename ...Args>
| typedef std::vector<std::function<T(Args...)>> functions;

Maybe through aliasing (since I know you can't template a typedef
statement)?

Öö Tiib

unread,
Aug 21, 2016, 7:38:46 AM8/21/16
to
On Sunday, 21 August 2016 06:43:50 UTC+3, eva galois wrote:
> Is it possible, through templates/aliasing('using')/etc, to create a
> container of functions with arbitrary return value and parameters?

It is hard to tell from one sentence what it is that you want.

People have several times made container of one of anything in C++.
Most popular of those is perhaps 'boost::any'. IMHO that thing is
bordering on uselessness, but we can certainly make a 'vector' of those
resulting with 'vector' of anything.

C++ thing borrowed from C that can be one of compile-time known set
is 'union'. Like most things borrowed from C it is constrained
and unsafe, but union of function pointers can still be what you want
and those can be put into 'vector'.

Lot of libraries have invented 'variant' classes that do same what 'union'
but more safely and with less constraints. Those are rather useful; I have
used 'boost::variant' in several projects.

>
> I know this code is basically gibberish, but the idea would be something
> like:
>
> | template <typename T, typename ...Args>
> | typedef std::vector<std::function<T(Args...)>> functions;
>
> Maybe through aliasing (since I know you can't template a typedef
> statement)?

That code tries to declare that "something" you want. That does not
actually illustrate what it is what you want. Also it is the angle
from what I rarely approach a problem. I commonly try to write down
how I want to use that "something". IOW ... what is the motivating
example to have that thing at all?


Richard Damon

unread,
Aug 21, 2016, 8:11:22 AM8/21/16
to
I thin the bigger issue is going to be how to USE the function
(pointers). It is easy to store them all in a container, you just need
to void (*)(void) and it is a simple container to store them.

The problem is that if you have stored an int f(int) and a float
g(float) in the container, this means that to use these the first you
need to pass and retrieve and int, and in the second a float. Thus the
usage site needs to know all the calling patterns to use it.

The alternative is, if the usage warrants it, is to wrap all the
functions in a templated wrapper that presents a uniform API, so the
call site is consistent, and now the container is storing all the same
type of function pointers.

Gert-Jan de Vos

unread,
Aug 21, 2016, 12:46:50 PM8/21/16
to

int add(int a, int b)
{
return a + b;
}

void f()
{
std::vector<std::function<void ()> vec;

int sum = 0;
vec.push_back([&]() { sum = add(1, 2); });
vec[0]();
}


Barry Schwarz

unread,
Aug 21, 2016, 3:16:27 PM8/21/16
to
On Sun, 21 Aug 2016 09:46:25 -0700 (PDT), Gert-Jan de Vos
<gert-ja...@onsneteindhoven.nl> wrote:

>
>int add(int a, int b)
>{
> return a + b;
>}
>
>void f()
>{
> std::vector<std::function<void ()> vec;

You need another > before vec

> int sum = 0;
> vec.push_back([&]() { sum = add(1, 2); });
> vec[0]();
>}
>

--
Remove del for email
0 new messages