I look at this grate code based on boost.Any and cant help but wonder if we could use Boost.Variant instead. I wonder if such API would be possible:
void voidFu
It is not possible. operator[]
is a run-time thing, while types are a compile-time thing. So should the compiler compile the following?
char const* str;
if (some_condition())
str = "voidFunc";
else
str = "stringFunc";
// ... some more code
if (some_condition())
funcs[str]();
else
funcs[str](str);
How is the compiler supposed to know whether the second call to some_condition()
gives the same result as before? Or whether the code in between modified the value of str
?
What about the following:
void call(some_map_like_type<std::string, boost::variant> const& funcs)
{
funcs["voidFunc"]();
}
How is the compiler supposed to know whether at call time funcs
contains an entry mapping "voidFunc"
to a function with no arguments? And what should happen if it is called once on with a value that does, and once with a value which doesn't?
Depending on what you actually want to achieve, there might be a way to get it with templates and constexpr
functions. However note that nothing which happens at runtime can affect whether the code compiles, for the simple reason that the code cannot be run before it is compiled.