I have a function and I need to test whether I can pass an argument of a given type to it. For example:
template<typename T, auto F>
decltype(F(declval<T>{})) foo();
Calling foo<int, bar>()
does 2 things:
- Sets the return type of
foo
would have the same return type asbar
- Ensures that
bar
is a function that accepts an argument of typeT
Unfortunately I don't have access to auto
template types, but I still want to accomplish both of these. What I need is a decltype
for function pointers, which would allow me to do something like this:
template <typename T, typename F>
decltype(declval<F>(declval<T>{})) foo();
So I could still call foo<int, bar>()
and get the same result. Of course there isn't a declval
for function pointers. But is there another way I could accomplish this?
declval
works on any type, including function pointers. – Mixiefoo<int, decltype(&bar), bar>()
? – Baobaobabstd::function
s :/ – Olivarezauto
I guess... but not my desired solution. – Katlinstd::function
to work around this? – Katlinbar
. Usingauto
just let me calldecltype
onF
. – Katlindeclval
works on all types as pointed out in Guillaume Racicot's answer. – Baobaobabdeclval
– Katlin