Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 1287

Answer by HolyBlackCat for Concept placement changes program outcome without compile error, possible to avoid?

$
0
0

This isn't directly about concepts. Any template placed there would have the same issue.

The first question you should ask yourself is: "Is SpecialCompare a customization point?".

If it is one (if the user can provide their own overloads), then your approach is flawed, because the rest of your code (not only the concept) might not see the user overloads, depending on the include order. You should switch to a different mechanism, such as:

  1. Replacing it with a class template and specializing it (like std::hash)

  2. Keeping the function, but making sure all calls go through ADL.

    This would require no changes to the code if only the owner of atype could provide SpecialCompare for it (in their namespace), buthere it's clearly not the case, because you're trying to provide itfor std::vector which you don't own.

    There is a hack to work around this: add a dummy parameter (e.g. struct tag {};) of a type declared in your namespace (global namespace in this case, but consider moving this to an actual namespace), which will make ADL consider both your namespace and the type's namespace.

Or, if SpecialCompare isn't a customization point, just order things the right way (the concept and all templates after all overloads of SpecialCompare).

In any case, document whether SpecialCompare is a customization point or not.


Viewing all articles
Browse latest Browse all 1287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>