Skip to content

Conversation

eisenwave
Copy link
Member

Related to #5641.

This section has a few issues:

  1. It contains wording "so that the function can select". This doesn't make much sense, since a function simply exists and never selects in itself. The intent is presumably that a function call selects the most efficient algorithm. That's how tag dispatch works anyway.

  2. The wording contains "compile time", which isn't a concept that really exists in C++. It is better to talk about overload resolution.

@jwakely
Copy link
Member

jwakely commented Mar 1, 2024

I disagree with the first point. A function such as std::distance can indeed select the best algorithm, by making a function call using the tag. That is, distance(a, b) calls distance_impl(a, b, cat(a)). Or maybe it instead does:

if constexpr (derived_from<cat_t<decltype(a)>, random_access_iterator_tag>)
  return b - a;

This selects the optimal implementation without a function call.

@eisenwave
Copy link
Member Author

Hmm, I guess you could view it that way as well. The way I see it, it still wouldn't be that the function selects the algorithm per-se, but the if constexpr block would end up calling the desired function directly by discarding all other options.

In practical terms, I'd also call if constexpr a form of "implementation selection" though. Perhaps the first part can be reverted and we can keep just 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants