Uninitialized variable using IIFE and fold expression in C++

132 Views Asked by At

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?

1

There are 1 best solutions below

5
Rlyeh On

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