Ok, I’m using Visual Studio 2010 to mess with lambdas in templates. VC++ has an odd quirk dealing with template parameters, but I found a workaround for calling a static function of a template parameter (T::magic in this example) by using the auto keyword. However, I have hit another hitch.
Say I have one of many classes with a function “magic”, and only the first 2 parameters matter for my call later. It may or may not have some defaulted parameters after the first 2. I don’t really care about them for this application, I only need to call magic(start, otherthing). Uhoh is one of these classes:
struct Uhoh
{
static int magic(char* start, int otherthing, bool doom = false);
}
I do the call with this template. I have to use a workaround of using the auto type to get T::magic for it to work in the lambda function.
template<typename T> void example()
{
auto themagic = T::magic;
std::function<int (char*)> test = [=](char* start) -> int
{
return themagic(start, 0);
};
test(0);
}
And then I call it or whatever.
int main()
{
example<Uhoh>();
}
I get an error about “too few arguments for call”, even though “magic” can take two arguments. Now, I can’t know for sure what the type will be for “otherthing” in any of the various “magic” functions. All I know is that 0 will be a valid value. Passing the type of whatever otherthing will be to the function “example” will be an extreme annoyance at best.
How can I properly type “themagic” so that VC++ doesn’t throw an error?
Here the information that the third parameter of
T::magichas default value is lost, as the type ofthemagicis deduced asint (*)(char*, int, bool), which cannot have any default value for the third parameter. So you cannot callthemagicwith only two arguments. You’ve to pass third argument as well.So do this:
Please note that default-value for a function parameter is not a part of the function signature, which means when you write
then type of
themagiccannot be deduced asint (*)(char*, int, bool=false).