Skip to content

[BUG] Warning C4702 unreachable code in ValueAccessor.h #2239

Description

@brandl-muc

Environment

Operating System: Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35223 for x86
Version / Commit SHA: SHA: [63a052f](
Other: compiled with /std:c++20 /permissive- (probably irrelevant)

Describe the bug

When using evalFirstCached() in ValueAccessor.h, MSVC emits the following warning: (see here)

5>[..]\openvdb\tree\ValueAccessor.h(1002,1): warning C4702: unreachable code

To Reproduce

Steps to reproduce the behavior:

  1. Include the header ValueAccessor.h without ignoring warnings
  2. Use evalFirstCached() such that Idx is less than template parameter Start
    alternatively, it must be greater than NumCacheLevels+1

Expected behavior

The compiler does not emit a warning

Additional context

The warning is triggered when any of the two constexpr-ifs conditions evaluates true.
In this case, the function is left early with a return, but the code will still continue to contain return this->isHashed<NodeType>(xyz); which indeed is unreachable for each such instantiation of the function template.

To fix this, change the code to this: (note the added else statements)

        return this->evalFirstPred([&](const auto Idx) -> bool
            {
                if constexpr(Idx < Start)                   return false;
                else if constexpr(Idx > NumCacheLevels+1)   return false;
                else
                {
                    using NodeType = typename NodeLevelList::template Get<Idx>;
                    return this->isHashed<NodeType>(xyz);
                }
            }, op);

Now, the culprit line is only contained in instantiations when both constexpr-ifs conditions evaluate false.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions