This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Add SFINAE guards to unique_function constructor.
ClosedPublic

Authored by sammccall on Feb 16 2021, 9:02 AM.

Details

Summary

We can't construct a working unique_function from an object that's not callable
with the right types, so don't allow deduction to succeed.
This avoids some ambiguous conversion cases, e.g. allowing to overload
on different unique_function types, and to conversion operators to
unique_function.

std::function and the any_invocable proposal have these.
This was added to llvm::function_ref in D88901 and followups

Diff Detail

Event Timeline

sammccall created this revision.Feb 16 2021, 9:02 AM
sammccall requested review of this revision.Feb 16 2021, 9:02 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 16 2021, 9:02 AM
kadircet added inline comments.Feb 16 2021, 10:56 AM
llvm/include/llvm/ADT/FunctionExtras.h
361

what about doing this in the constructor of the UniqueFunctionBase instead?

llvm/unittests/ADT/FunctionExtrasTest.cpp
267

s/function_ref/unique_function/

here and below

270

looks like having multiple tests with the same name is a failure on some buildbots :/

s/FunctionRefTest/UniqueFunctionTest/

sammccall updated this revision to Diff 324092.Feb 16 2021, 1:16 PM
sammccall marked 2 inline comments as done.

fix test

llvm/include/llvm/ADT/FunctionExtras.h
361

I don't think this works even in principle, only the signatures are taken into account when resolving overloads, and the base-initialization is not part of the signature.
https://godbolt.org/z/TWvcsE - see that the overload on the class with the sfinae constructor template works, but overloading on types derived from it is ambiguous.

Practically it also doesn't work because UniqueFunctionBase doesn't have access to R and P..., and you can't pass explicit template args to a constructor template.

(Or maybe I'm misunderstanding your suggestion)

llvm/unittests/ADT/FunctionExtrasTest.cpp
267

Oops... need to do a *little* more than copy/paste...

kadircet accepted this revision.Feb 17 2021, 12:20 AM

thanks, lgtm!

llvm/include/llvm/ADT/FunctionExtras.h
361

no you understood correctly, i was missing the fact that base initialization required template instantiation. so nvm.

This revision is now accepted and ready to land.Feb 17 2021, 12:20 AM
This revision was automatically updated to reflect the committed changes.