Click here to Skip to main content
12,406,342 members (31,386 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ STL Lambda
Hi, All

Recently I try to use std::function and Lambda expression to simply my code.
However there are some weird problems.

#include <cstdio>
#include <vector>
#include <functional>

int main(int argc, char *argv[])
{
	std::function<bool (int)> arr[]={
		[&](int n)->bool {
			std::wstring y;
 
			printf("function 0, %d\n", n);
			return false;
		},
   
		[&](int n)->bool {
			std::wstring y;
 
			printf("function 1, %d\n", n);
			return false;
		}
	};
  
	arr[1](100);
	return 0;
}

Above code can be compiled by VS2010, but crush during run-time.
I found the internal pointer _Impl of function template holds invalid address.

If I define an array in lambda functions, the program crashes at runtime with C1001.

The following code, does not crash, only difference, used int x[100] instead of std::wstring y:
#include <cstdio>
#include <vector>
#include <functional>

int main(int argc, char *argv[])
{
	std::function<bool (int)> arr[]={
		[&](int n)->bool {
			int x[100]={0};
 
			printf("function 0, %d\n", n);
			return false;
		},
   
		[&](int n)->bool {
			int x[100]={0};
 
			printf("function 1, %d\n", n);
			return false;
		}
	};
  
	arr[1](100);
	return 0;
}

Any advice will be appreciated.
Posted 1-Jan-13 23:23pm
Updated 2-Jan-13 5:20am
v3
Comments
armagedescu 2-Jan-13 11:14am
   
I think the destructor of std::wstring does that. The problems appears with constructing/destructing and new/delete operators. Seems like arrays of std::function are not supported, or not well supported yet.
   
Crash? I believe there is not crash, more likely, this is unhandled exception. Could you simply execute this under the debugger and comment in what line the exception was thrown?
—SA
armagedescu 3-Jan-13 5:25am
   
Is not unhandled exception. It is Access Violation while calling of a member function by invalid pointer.
   
Thank you; Maximilien already told me about compiler crash. Too bad.
—SA
armagedescu 3-Jan-13 12:07pm
   
So, as I see, there are two problems.
1. Program can't be compiled because compiler crashes.
2. If compiler can compile the program, then the program crashes at runtime.
In both cases seems to be like bugs in VisualStudio 2010 and above.
Maximilien 2-Jan-13 14:04pm
   
Funny, the second code snipped crashes the compiler on VS2012 (Release Candidate, july version).
(nothing else to add, sorry).
   
Oh, a compiler? This is too bad. And yet they say, C++11, C++11... :-)
—SA
Maximilien 2-Jan-13 14:05pm
   
Oh, yes ... I have something to add.
Was all of this (lambdas, ... ) well defined and implemented in the VS2010 compiler ?
CPallini 3-Jan-13 3:51am
   
Your code, after the missing #include <string> was fixed, compiled and ran fine using g++-4.7.
CPallini 3-Jan-13 16:21pm
   
I tested your code with Visual Studio Professional 2012 Update 1. It compiles and runs fine.
Bruce Hsu 3-Jan-13 20:58pm
   
I guess Microsoft had give up VS2010.
No any update for this issue in official site.
   
Really. That's sad...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160730.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100