Lambda function cannot call static functions of function template parameter

Ok, so I have something basically like this:

template<typename T> void example() { std::function<int (byte*)> test = [=](byte* start) -> int { return T::magic(start); } } 

Ignoring how β€œunclean” makes these bare calls, he also does not compile, giving these errors:

 'T' : is not a class or namespace name 'magic': identifier not found 

Is there a way to make a call for a typical name of type T, assuming I will always call example () with a class that has a magic (byte * start) function? Of course, I do not need to update this template function for every single class that will do this.

I do this in VC ++ 2010, and it looks like it might be a compiler error. Possible workarounds?

+6
source share
3 answers

The only mistake is the missing semicolon. Once this is fixed, it works great.

 #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>(); } 

http://ideone.com/dRdpI

This seems to be a bug in the implementation of lambda VC10, 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(); } 
+4
source

I reproduced the problem with VS2010. You should call the example function, though:

 #include <functional> struct SomeT { static int magic(unsigned char*) { return 42; } }; template<typename T> void example() { std::function<int (unsigned char*)> test = [=](unsigned char* start) -> int { return T::magic(start); }; } int main() { example<SomeT>(); } 

Update based on OP comment:

It works:

 #include "stdafx.h" #include <functional> struct SomeT { static int magic(unsigned char*) { return 42; } }; template<typename T> void example() { auto func = T::magic; std::function<int (unsigned char*)> test = [=](unsigned char* start) -> int { return func(start); }; } int main() { example<SomeT>(); } 

I was looking for workarounds, but so far nothing is working, I tried this nice permutation, including this nice permutation, but so far have failed:

 template<typename T> void example() { static const T* workaround; std::function<int (unsigned char*)> test = [=](unsigned char* start) -> int { typedef decltype(*workaround) innerT; return innerT::magic(start); }; } 

Hard ... Strike>

+3
source

I am sure that you are doing something wrong, because I can call the static method using the T::f() syntax inside the lambda:

 #include <iostream> #include <functional> struct A { static void f() { std::cout << "called" << std::endl; } }; template<typename T> void example() { std::function<void()> test = [=]() { T::f(); }; test(); } int main() { example<A>(); return 0; } 

Demo: http://ideone.com/IPakS

Therefore, please write more information. So far this is the answer. Try comparing your code with this and see if you are doing something terribly wrong.

If you use GCC, did you compile your code with the -std=c++11 option?

0
source

Source: https://habr.com/ru/post/901387/


All Articles