https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86465

--- Comment #16 from Geoff <geoff3jones at googlemail dot com> ---
This appears to still be an issue in gcc 11.2 minimal reproduced example using
std::optional https://godbolt.org/z/ebbMe3Wva.

-std=c++17 -O3 -fsanitize=address -Wall -Werror -Wno-unused

std::optional<bool> empty_optional_or_true(int x){
    if(x > 0){
        return std::make_optional(true);
    }
    return {};
    //      ^ error here
}

int main(){
    empty_optional_or_true(0)
}

------------

In function 'std::optional<bool> empty_optional_or_true(int)',
    inlined from 'std::optional<bool> empty_optional_or_true(int)' at
<source>:28:21:
<source>:7:13: error: '*(unsigned char*)((char*)&<unnamed> +
offsetof(std::optional<bool>,std::optional<bool>::<unnamed>.std::_Optional_base<bool,
true, true>::<unnamed>))' may be used uninitialized
[-Werror=maybe-uninitialized]
    7 |     return {};
      |             ^
<source>: In function 'std::optional<bool> empty_optional_or_true(int)':
<source>:7:13: note: '<anonymous>' declared here
    7 |     return {}; // <- second here
      |    

In the linked example it can be see that making the global function static or
wrapping it in an anonymous namespace will cause the error to disappear.

Reply via email to