2

I want automatic deduction of the arguments of a templated function which accepts a function, while using lambdas. This Example shows some of my options:

template <class T>
void foo(void (*func)(T)) {
    T val;
    // do something with val and func...
}

int main() {
    auto pfunc0         =  [] (int) { /*...*/ };
    void (*pfunc1)(int) =  [] (int) { /*...*/ };
    auto* pfunc2        = +[] (int) { /*...*/ };

    foo(pfunc0);      // not ok
    foo<int>(pfunc0); // ok, but redundant
    foo(pfunc1);      // ok, but redundant
    foo(pfunc2);      // ok
}

pfunc2 uses a trick I learned here: Obtaining function pointer to lambda?. So actually I should be happy with the pfunc2 case as it is concise and non repeating code, unfortunately the Visual C++ 2012 IDE complains it was erroneous code even though it compiles just fine.

Are there any workarounds or recommendations for this problem?

IDE error messages:

In the "auto* pfunc2" line: The IDE underlines 'auto' and says

Error: cannot deduce 'auto' type

also it underlines '[' where it complains

Error: more than one conversion function from "lambda[]void (int)->void" to a build-in type applies:
function "lambda[]void (int)->void::operator void (*)(int)() const"  
function "lambda[]void (int)->void::operator void (*)(int)() const"  
function "lambda[]void (int)->void::operator void (*)(int)() const"
Community
  • 1
  • 1
testman
  • 248
  • 3
  • 14
  • I don't have access to VS2012, but have you tried `foo(+pfunc0);`? – krzaq Nov 06 '16 at 20:43
  • @krzaq I updated my question with the error messages. I tried your suggestion but it did not change anything (same error messages). – testman Nov 06 '16 at 20:52
  • @testman My bad, I completely missed that! – Quentin Nov 06 '16 at 21:12
  • @Quentin I updated my question with pictures of the error messages. – testman Nov 06 '16 at 21:15
  • @krzaq I read your now deleted answer, assuming there is no workaround, is it ok to just ignore that the IDE complains or is that discouraged? I could always use the more verbose code if it really was a problem. – testman Nov 06 '16 at 21:23
  • @testman the code is okay and the bug that causes those problem is fixed in a newer compiler version. honestly, I find it weird that you hadn't hit it, but if the correct code works - don't listen to buggy IDE. – krzaq Nov 06 '16 at 21:24
  • @krzaq What was wrong with your answer then? If there is nothing else I can do I would accept that, also "I find it weird that you hadn't hit it" what do you mean with that? – testman Nov 06 '16 at 21:28
  • @testman the problem with the answer is that the bug I linked is about compile errors, not just IDE ones. – krzaq Nov 06 '16 at 21:29
  • @Yakk Thanks for hinting that, you are correct. – testman Nov 07 '16 at 12:03
  • @Yakk I don't know if i should re-edit your edit: I needed to escape the asterisks to not make the text italic, now that you edited there are still the backslashes. Thanks for the nice formatting though! – testman Nov 07 '16 at 14:21
  • @testman Ah, the `\` was an escape. When you are injecting code, the "right" way is to make it code, not escaped plaintext. Compiler error messages, in my experience, are best formatted as code (as they like fixed-width fonts and contain code segments) – Yakk - Adam Nevraumont Nov 07 '16 at 14:30

1 Answers1

1

This is related to this bug (closed as "by design"). VC++ supports several calling conventions on x86 and lambdas with empty capture lists provide conversions to them all. That's why there's ambiguity.

Unfortunately, there's no workaround listed that you haven't already tried.

By the way, this bug is listed as fixed in Visual C++ 2015 Update 2

krzaq
  • 16,240
  • 4
  • 46
  • 61
  • I installed Visual Studio Community 2015 Version 14.0.25431.01 Update 3 now not only the IDE but also the compiler complains :/ – testman Nov 07 '16 at 15:26
  • @testman I recall there being a naming mixup with Visual Studio 2015, with two versions being named very similarly being different things: https://pay.reddit.com/r/cpp/comments/56ecje/psa_visual_studio_15_and_visual_studio_2015_are_2/ Maybe you're a victim of that? – krzaq Nov 07 '16 at 16:06