I have checked out std::enable_if to conditionally compile a member function
However it doesn't work for me. I need to restrict T
of a class method to some types.
template<typename T = typename enable_if_t<
is_same_v<T, long> || is_same_v<T, int> || is_same_v<T, double>
|| is_same_v<T, float> || is_same_v<T, size_t>>
>
shared_ptr<Node<T>> LinkedList<T>::AddNumbers(
shared_ptr<Node<T>> p1, shared_ptr<Node<T>> p2, T carry)
{
<snip>
}
I get build error:
identifier T is undefined
I am using C++20. Any advice and insight is appreciated.
I try out concepts
suggested by @JeJo, but get the following build error on the line performing arithmetics:
error C2676: binary '/': 'T' does not define this operator or
a conversion to a type acceptable to the predefined operator
I have the template class declaration in header file and implementation in .cpp
file. Header file:
template <typename T> class LinkedList
{
public:
<snip>
shared_ptr<Node<T>> AddNumbers(
shared_ptr<Node<T>>, shared_ptr<Node<T>>, T carry = 0);
};
When I use the suggestion by @JeJo, I bump into
error C3855: 'LinkedList<T>': template parameter 'T' is
incompatible with the declaration