Skip to content

MathUtils: let bringTo* deduce the type of the call they forward to - #15946

Merged
ktf merged 1 commit into
AliceO2Group:devfrom
ktf:pr15946
Oct 12, 2026
Merged

ktf merged 1 commit into
AliceO2Group:devfrom
ktf:pr15946

Conversation

@ktf

@ktf ktf commented Oct 12, 2026

Copy link
Copy Markdown
Member

The parameter is a reference, so T is deduced with the address space attached,
and forcing that same T on a by-value parameter is a substitution failure on
Metal. Letting the inner call deduce its own type gives the same T everywhere
else, where the address space is not part of it.

The parameter is a reference, so T is deduced with the address space attached,
and forcing that same T on a by-value parameter is a substitution failure on
Metal. Letting the inner call deduce its own type gives the same T everywhere
else, where the address space is not part of it.
@ktf
ktf requested a review from shahor02 as a code owner October 12, 2026 14:54
@davidrohr

Copy link
Copy Markdown
Collaborator

But why is this needed? The existing code is even more explicit, so what is the problem?

@ktf

ktf commented Oct 12, 2026 •

Copy link
Copy Markdown
Member Author

As I understand it, bringTo02Pi(T& phi) deduces T as e.g. thread float — the address space rides along into T because the parameter is a reference, much like const would. But the explicit <T> then can't be fixed by matching it: to02Pi takes its parameter by value, and a by-value parameter may not be address-space qualified at all. Dropping the explicit <T> lets the inner call deduce its own T, which comes out as plain float because deducing a by-value parameter drops top-level qualifiers.

This is similar to:

template <typename T>
T to02Pi(T phi)          // by value, and it modifies a local of type T
{
  T res = phi;
  if (res < 0.f) {
    res += 6.283185307f;
  }
  return res;
}

template <typename T>
T wrap(T& phi)           // reference, so T picks up the qualifier
{
  return to02Pi<T>(phi); // forwards the reference-deduced T
}

int main()
{
  const float phi = -1.f;
  return (int)wrap(phi); // T = const float
}

which fails because the inner T is deduced as const float (whose mutation the compiler complains about). Dropping the <T> makes it work also in this case because the const is ignored when passed by value.

@ktf
ktf merged commit b45ab11 into AliceO2Group:dev Oct 12, 2026
8 of 9 checks passed
@ktf

ktf commented Oct 12, 2026

Copy link
Copy Markdown
Member Author

Builds. Merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants