Suppose I have something like:
template <class T>
void do_something(T t){
pass_it_somewhere(t);
t->do_something();
}
Now it would be useful that T
is allowed to be a pointer- or a non-pointer type. Function do_something(...)
can basically handle pointers and non-pointers, except for the t->do_something()
. For pointers, I would need a ->
and for non-pointers, I would need a .
to access members.
Is there a way to make T
accept pointers and non-pointers?
Which parameter is legal for non-type template? Explanation: The following are legal for non-type template parameters:integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.
A template has only one type, but a specialization is needed for pointer, reference, pointer to member, or function pointer types. The specialization itself is still a template on the type pointed to or referenced.
A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
A non-type template argument provided within a template argument list is an expression whose value can be determined at compile time. Such arguments must be constant expressions, addresses of functions or objects with external linkage, or addresses of static class members.
You could create a dereference mechanism as below:
template<typename T>
std::enable_if_t<std::is_pointer<T>::value, std::remove_pointer_t<T>&> dereference(T& t) {
return *t;
}
template<typename T>
std::enable_if_t<!std::is_pointer<T>::value, T&> dereference(T& t) {
return t;
}
and use it in your function as:
template <class T>
void do_something(T t){
pass_it_somewhere(dereference(t));
dereference(t).do_something();
}
Live Demo
This way you'll have to do only with concrete versions of T
.
Soultion 1
Use template specialization:
template <class T>
void do_something(T t){
pass_it_somewhere(t);
t.do_something();
}
template <class T>
void do_something(T* t){
pass_it_somewhere(t);
t->do_something();
}
Solution 2
Add a user-defined pointer operator in class T:
class A
{
public:
void do_something() const {}
const A* operator->() const { return this; }
};
template <class T>
void do_something(T t){
pass_it_somewhere(t);
t->do_something();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With