Skip to content

Suggestion: put else logic for if-constexpr-return into else scope if possible #5605

@achabense

Description

@achabense

Godbolt example

Unoptimized build will generate dead code for code like return rand(); even if it will never be executed. The dead code can be avoided by enclosing in else scope (see fooB in the godbolt example).

template<int i>
int fooA(){
    if constexpr(i<10){
        return 0;
    }
    return rand();
}

There are some patterns like this in the current library. For example, the following lines can be enclosed in else scope.

STL/stl/inc/xhash

Line 2053 in 313964b

return true;

++_It._Get_current();

const auto _Validate = [&] {

And this is not used by all constexpr return branches:
tm _Time;

And these (though consteval; for style consistency) can be made into else branch:
return {_St::_None};

return {_St::_None};

And due to this, I guess is_constant_evaluated() is causing a lot of unavoidable dead code in debug build. See fooC in the godbolt example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSomething can be improvedfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions