Click here to Skip to main content
Rate this: bad
good
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 2-Jan-13 0:23am
Edited 2-Jan-13 6:20am
(no name)5.7K
v3
Comments
armagedescu at 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.
Sergey Alexandrovich Kryukov at 2-Jan-13 12:16pm
   
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 at 3-Jan-13 5:25am
   
Is not unhandled exception. It is Access Violation while calling of a member function by invalid pointer.
Sergey Alexandrovich Kryukov at 3-Jan-13 11:27am
   
Thank you; Maximilien already told me about compiler crash. Too bad.
—SA
armagedescu at 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 at 2-Jan-13 14:04pm
   
Funny, the second code snipped crashes the compiler on VS2012 (Release Candidate, july version).
(nothing else to add, sorry).
Sergey Alexandrovich Kryukov at 2-Jan-13 23:07pm
   
Oh, a compiler? This is too bad. And yet they say, C++11, C++11... :-)
—SA
Maximilien at 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 at 3-Jan-13 3:51am
   
Your code, after the missing #include <string> was fixed, compiled and ran fine using g++-4.7.
CPallini at 3-Jan-13 16:21pm
   
I tested your code with Visual Studio Professional 2012 Update 1. It compiles and runs fine.
Bruce Hsu at 3-Jan-13 20:58pm
   
I guess Microsoft had give up VS2010.
No any update for this issue in official site.
Sergey Alexandrovich Kryukov at 3-Jan-13 21:29pm
   
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
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2014
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