1

I was playing around with IIFE's and parameter packs in MSVC C++20 when I stumbled across a strange error. The following code is a minimal reproducible example.

#include <functional>
#include <iostream>

template <typename... Ts>
struct Foo {
    std::function<void(Ts...)> fnc = {};
    Foo() {}
    Foo(const Foo&) : fnc(
        [](Ts... ts) {
            bool test = ([&ts](){ return true; }() && ...);
            std::cout << test << std::endl;
        }
    ) {}
};
int main() {
    Foo<int> f;
    Foo<int> f2 = f;
    return 0;
}

This gives the following error(s):

error C4700 uninitialized local variable 'test' used
fatal error LNK1257: code generation failed

which does not make sense to me. What's even more strange is that making the copy constructor a default constructor gets rid of the error. Should this be the case, or is there a bug in the compiler?

BubLblckZ
  • 434
  • 4
  • 14
  • What compiler are you using? What compilation flags? Please post the full error message instead of describing it. – Quimby Jun 04 '23 at 10:58
  • 4
    Probably a MSVC bug. Both GCC and Clang compile this just fine. – Nelfeal Jun 04 '23 at 11:01
  • "_the following error_": It is not an error, just a warning. MSVC still compiles the code. The warning is a false positive. I don't see anything wrong with the code. – user17732522 Jun 04 '23 at 11:17
  • @user17732522 Sorry for being unclear. It follows up with a LINK error, which I have now added. – BubLblckZ Jun 04 '23 at 11:29
  • @BubLblckZ It links fine on compiler explorer as well (note that I enabled Output... -> Link to binary) https://godbolt.org/z/q8jTTf7d3. There should be another error message from the linker if linking fails. C4700 is from the compilation step. – user17732522 Jun 04 '23 at 11:46
  • What version of MSVC are you using? I only get this error in godbolt. – Rlyeh Jun 04 '23 at 11:59
  • msvc 19.latest gives the warning but msvc 19.35 doesn't... – Rlyeh Jun 04 '23 at 12:04
  • @user17732522 True; after removing some flags I originally used it does build with only the warning, however, the program behaves as if the variable is actually uninitialized, so there still seems to be a bug there. In older versions of MSVC there is no bug though, so probably just a bug in the latest version. – BubLblckZ Jun 04 '23 at 12:21

1 Answers1

2

Uniform initialization appears to not trigger the warning

bool test{ ([&ts](){ return true; }() && ...) };

https://godbolt.org/z/8fvfnsfh1

Edit: Still gives the wrong result on MSVC 19.36

Rlyeh
  • 193
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 04 '23 at 11:18
  • Although this makes even less sense, it does seem to fix the error, so thanks! – BubLblckZ Jun 04 '23 at 11:25
  • @BubLblckZ Switching to msvc 19.35 in godbolt seems to fix it too, I can't get the warning to trigger in VS2022. – Rlyeh Jun 04 '23 at 12:15
  • @Rlyeh Yep, thanks for the help, seems to be a bug in the latest version of msvc. – BubLblckZ Jun 04 '23 at 12:17
  • @BubLblckZ Just checked it out and my solution gives the wrong results in the latest version. – Rlyeh Jun 04 '23 at 12:22