-1

Suppose I have a variadic function like this:

template <typename... A>
void func(A... args)
{
    //Do stuff
}

And I have also a vector like this:

enum Type{
    DOUBLE,
    STRING
};
std::vector<std::pair<std::string, Type>> varg;

So I want to iterate over the elements of the map and call the templated function with the specified types, like: varg = {{"a", Type::STRING}, {"1.2", Type::DOUBLE}}; should call func("a", 1.2)

Any hints on how to do that?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Fynn
  • 811
  • 2
  • 10
  • 19

2 Answers2

1

You are mixing up compile-time and runtime features here. (Hint: It won't work)
As a thought-experiment:

Suppose you have a std::vector<> of some kind, that is filled with some data, say with N entries. Note that this N is a priori only known at runtime! Now you want to unpack these N data segments into a variadic template, something that has to be done compile-time! Templates are compile-time features!

This cannot work. You can in principle do something similar if you use compile-time containers (see boost::mpl). But these require, that you know (or can calculate) everything at compile-time.
Also you may want to look at boost::fusion which sort of tries to bridge this gap. Though you should always note that it cannot! Templates are still compile-time only.

iolo
  • 1,090
  • 1
  • 9
  • 20
1

At the lowest level (C-like), the application binary interface follows some calling conventions, notably how arguments are transmitted to a function, and it usually depends on the type of the argument (often, pointers go in some processor registers and floating point numbers go in some other kind of registers).

If you want to call a function of unknown signature (i.e. you know its signature and the actual arguments and their number only at runtime, not at compile time) you need some ABI specific tricks, and probably some machine specific code. You might consider using the libffi which provides

a portable, high level programming interface to various calling conventions

BTW, you could consider packing or boxing your values in some "universal" container à la boost::any or QVariant or your own tagged unions, or perhaps boost::variant

Perhaps you might want to embed some interpreter in your application, e.g. GNU guile or Lua.

Noticve that for C or C++, functions and function pointers have some compile-time known signature which matters a lot. Calling something with the wrong signature (that is a function whose signature is not the one the compiler expects) is undefined behavior.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I feel like there's a better solution to the problem then ABI hacks, OP is just asking the wrong question. – TartanLlama Jan 26 '16 at 10:28
  • I can know in runtime, how the function is gonna be called (and the signature doesn't change from now and on), I don't know if that helps as well – Fynn Jan 26 '16 at 10:37
  • 1
    @Fynn The thing is, if you want to use templates, every single template parameter has to be known at compile-time. – iolo Jan 26 '16 at 11:02