Ok, so I have something setup basically like this:
template<typename T> void example()
{
std::function<int (byte*)> test = [=](byte* start) -> int
{
return T::magic(start);
}
}
Ignoring how "unclean" making these naked calls is, it also doesn't compile, giving these errors:
'T' : is not a class or namespace name
'magic': identifier not found
Is there any way to be able to make a call on the generic typename T, assuming I will always be calling example() with a class that has the function magic(byte* start)? Surely I don't have to redeclare this template function for every single class that will be doing this.
I'm doing this in VC++ 2010, and it appears it may be a compiler bug. Any possible workarounds?
The only error there is the missing semi-colon. Once that is fixed, it works fine.
#include <iostream>
#include <functional>
typedef unsigned char byte;
template<typename T> void example()
{
std::function<int (byte*)> test = [=](byte* start) -> int
{
return T::magic(start);
}; // <--------------------------------- You were missing that
}
struct Foo {
static int magic(byte*);
};
int Foo::magic(byte* start)
{
std::cout << "magic\n";
}
int main()
{
example<Foo>();
}
As this appears to be a bug in VC10's lambda implementation, a possible workaround is to create a local functor class:
template<typename T> void example()
{
struct Foo {
int operator()(byte * start) { return T::magic(start); }
};
std::function<int (byte*)> test = Foo();
}