Reputation: 601
I'd like to specialize a template function to take non-pointer types and if in case it gets a pointer I'd like to call it without the *.
I'm wondering if there's a way without using std::remove_pointer.
for example I'd like to do something like this:
template<typename T>
void setName() {
name = __PRETTY_FUNCTION__;
}
template<typename T>
void setName<T*>() {
setName<T>();
}
name
is defined as a private data member.
Upvotes: 1
Views: 203
Reputation: 666
Your idea is correct but partial function template specialization is not allowed in C++. Fortuately partial class template specialization is allowed so you can use static method workaround (see specialization of setName_impl
below) and if needed function template wrapper (see setName()
below):
template<typename T>
struct setName_impl{
static void exec() { }
};
template<typename T>
struct setName_impl<T*>{
static void exec() {
setName<T>::exec();
}
};
template<typename T>
void setName() {
setName_impl<T>::exec();
}
Upvotes: 3