C++ Logo

STD-DISCUSSION

Advanced search

Subject: Non-type template argument issue
From: Edward Diener (eldlistmailingz_at_[hidden])
Date: 2021-02-03 11:36:22


I have run into the problem where a partial specialization of a class
template involves a non-type parameter whose type depends on a type
parameter:

template <class T, T * t >
class ATemplate { /* some code... */ } ;

If I try to partially specialize this, where t is a nullptr, as in:

template <class T>
class ATemplate<T,nullptr> { /* some code... */ } ;

I run into a compiler error best explained to me by the entry at
https://en.cppreference.com/w/cpp/language/partial_specialization where
it says:

"5) Non-type template argument cannot specialize a template parameter
whose type depends on a parameter of the specialization"

I am sorry that I do not know in the C++ standard where to find the
above rule as mentioned in the cppreference entry.

While I understand the practical reason for the rule, it seems to me
that in the case above the rule is poor. This is because no matter what
T may be the partial specialization, with 't' as a nullptr would almost
always involve valid code since a nullptr can point to any T.
Furthermore in the situation above the only way that the code could
handle the possibility of a nullptr being passed as an argument for "T *
t" in the code of the primary template would be run-time based, where
clearly I might want to provide a partial specialization so that member
functions in the primary template where a nullptr based 't' are
incorrect could be removed completely. In other words I would very much
like to handle a nullptr based 't' at compile time rather than at
run-time, and I see no way to do this.

Is it possible that the rule cited in cppreference is too rigid, in that
in the case of a non-type template parameter of pointer type a partial
specialization with nullptr should be satisfactory no matter what the
type parameter ends up being ?


STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups