I'm trying to use C++ lambdas for the first time.
My code looks like the following:
struct ImportModeInfo
{
CImportTransactions::ImportMode Mode;
LPCTSTR pszDisplayName;
CImportTransactions *(*pFactory)(LPCTSTR pszDataFile, LPCTSTR pszCsvFile, LPCTSTR pszLogFile);
};
CImportTransactions::ImportModeInfo CImportTransactions::ImportModeTable[] =
{
{ CImportTransactions::ImportMode::GasBoy, _T("GasBoy"), [](LPCTSTR p1, LPCTSTR p2, LPCTSTR p3) { return new CImportGasBoyTransactions(p1, p2, p3); } },
{ CImportTransactions::ImportMode::Opw, _T("OPW/AFC"), [](LPCTSTR p1, LPCTSTR p2, LPCTSTR p3) { return new CImportOpwTransactions(p1, p2, p3); } },
};
As it is, Intellisense highlights the [
starting the capture list, giving me the following error:
more than one conversion function from "lambda []CImportGasBoyTransactions *(LPCTSTR p1, LPCTSTR p2, LPCTSTR p3)->CImportGasBoyTransactions *" to "<error-type>" applies:
function "CImportTransactions::lambda []CImportOpwTransactions *(LPCTSTR p1, LPCTSTR p2, LPCTSTR p3)->CImportOpwTransactions *::operator CImportOpwTransactions *(*)(LPCTSTR p1, LPCTSTR p2, LPCTSTR p3)() const"
function "CImportTransactions::lambda []CImportOpwTransactions *(LPCTSTR p1, LPCTSTR p2, LPCTSTR p3)->CImportOpwTransactions *::operator CImportOpwTransactions *(*)(LPCTSTR p1, LPCTSTR p2, LPCTSTR p3)() const"
function "CImportTransactions::lambda []CImportOpwTransactions *(LPCTSTR p1, LPCTSTR p2, LPCTSTR p3)->CImportOpwTransactions *::operator CImportOpwTransactions *(*)(LPCTSTR p1, LPCTSTR p2, LPCTSTR p3)() const"
function "CImportTransactions::lambda []CImportOpwTransactions *(LPCTSTR p1, LPCTSTR p2, LPCTSTR p3)->CImportOpwTransactions *::operator CImportOpwTransactions *(*)(LPCTSTR p1, LPCTSTR p2, LPCTSTR p3)() const"
I really don't understand what any of this means. I don't see that my lambda expressions are using any variables other than the three that are passed to them. It's true I allocate a new object, but again that is from inside the lambda.
If I change []
to [=]
or [&]
, the error goes away. Can anyone explain why either of these changes would be needed in this case?
UPDATE: Actually, it looks like I still get the following error using [&]
or [=]
, or changing my LPCTSTR
parameter types to auto
. Can anyone see what I am doing wrong?*
'initializing': cannot convert from '<lambda_4ea6e84698d4bd1ce2c6d0d3f1bf1ccc>' to 'CImportTransactions *(__cdecl *)(LPCTSTR,LPCTSTR,LPCTSTR)'
UPDATE 2: I also tried changing my lambda as follows (note the typecast on the return value). (Note that both CImportGasBoyTransactions
and CImportOpwTransactions
derive from CImportTransactions
, so I don't see why the type cast is needed.)
CImportTransactions::ImportModeInfo CImportTransactions::ImportModeTable[] =
{
{ CImportTransactions::ImportMode::GasBoy, _T("GasBoy"), [](LPCTSTR p1, LPCTSTR p2, LPCTSTR p3) { return (CImportTransactions*)new CImportGasBoyTransactions(p1, p2, p3); } },
{ CImportTransactions::ImportMode::Opw, _T("OPW/AFC"), [](LPCTSTR p1, LPCTSTR p2, LPCTSTR p3) { return (CImportTransactions*)new CImportOpwTransactions(p1, p2, p3); } },
};
The result, this seems to eliminate the previous errors. But now I get the following error:
fatal error C1001: An internal error has occurred in the compiler.
(compiler file 'f:\dd\vctools\compiler\utc\src\p2\p2symtab.c', line 7154)
To work around this problem, try simplifying or changing the program near the locations listed above.
Oh boy, aren't I glad I'd give C++ lambdas a try this evening???