Reputation: 64895
I have a function which accepts as a non-type template parameter a pointer O
to an arbitrary object (with linkage), like so:
Foo foo;
...
template <typename T, T* O>
ftype* make_function() {
// do something with O
}
To pass the pointer to an foo
object of type Foo
, I need to call it like this:
make_function<Foo, &foo>()
However, I'd like to simply call it like make_function<&foo>()
, i.e., have the type Foo
of the pointer deduced, as it could be in similar patterns (e.g., if I was passing foo
as an argument).
Here's a more fleshed out example on Godbolt.
Upvotes: 1
Views: 52
Reputation: 217135
C++17 allows
template <auto* O>
ftype* make_function() {
// do something with O
}
Before that, you need indeed
template <typename T, T* p>
Macro can help to reduce verbosity at usage:
#define AUTO(p) decltype(p), p
// ...
make_function<AUTO(&foo)>();
Upvotes: 4