When using non-type template parameters, the non-type parameter must be ‘structural’ per the C++ standard. Classes with private data members are not structural. However, sometimes it is convenient to use private data members to protect a class invariant. Having a ‘structural’ restriction seems too restrictive. Consider the following code:
class A
{
int data_priv;
public:
int const& data() const { return data_priv; }
int & data() { return data_priv; }
};
class B
{
public:
int data;
};
template<A a> void funA() { } // Error: A is not structural
template<B b> void funB() { } // OK
int main()
{
funA<{}>();
funB<{}>();
};
What stops compilers from implementing (or the C++ standard from specifying) non-type template parameters with private data members?
In case there is some difficulty with comparing classes with private data members, would not be enough to define the comparison operator to allow non-type template parameters with private data members?