c++ - Why is initialization of a constant dependent type in a template parameter list disallowed by the standard? -
in answer post "(partially) specializing non-type template parameter of dependent type", states:
the type of template parameter corresponding specialized non-type argument shall not dependent on parameter of specialization. [ example:
template <class t, t t> struct c {}; template <class t> struct c<t, 1>; // error template< int x, int (*array_ptr)[x] > class {}; int array[5]; template< int x > class a<x,&array> { }; // error
—end example ]
my question why restriction here? there @ least 1 use case find restriction interferes writing clean code. e.g.
template <typename t, t*> struct test; template <typename t> struct test<t, nullptr> // or struct test<t, (t*)nullptr> { }; template <typename r, typename...args, r(*fn)(args...)> struct test<r(args...), fn> { };
though i'm unsure if there other cases stating constant based on type problem beyond not making sense.
anyone have reason why so?
(imho) common reasons standard disallows specific feature are:
- the feature covered mechanism in language, rendering superfluous.
- it contradicts existing language logic , implementation, making implementation potentially code breaking.
- legacy: feature left out in first place , we've built lot without it's forgotten (see partial function template specialization).
difficulty of implementation factor, though may take time compiler implementations catch evolution on "hard" stuff.
you wrap non type template parameter in type:
template < typename t1, typename t2 > struct demo {}; // primary template template < typename t > struct demo<t, integral_constant<t, 0>> {}; // specialization
i doubt this hack falls case 1. case 3 possibility lets examine case 2. this, have know related rules standard imposes on class templates partial specializations.
14.5.5 class template partial specializations
a non-type argument non-specialized if name of non-type parameter. other non-type arguments specialized. (c1)
within argument list of class template partial specialization, following restrictions apply:
- a partially specialized non-type argument expression shall not involve template parameter of partial specialization except when argument expression simple identifier. (c2)
- the type of template parameter corresponding specialized non-type argument shall not dependent on parameter of specialization. (c3)
i marked first 3 clauses found relevant (the third 1 in question). according c1 in our case we have specialized non-type argument c2 should stand, yet
template <class t, t t> struct c {}; template <class t> struct c<t, 1>;
is
template <class t, t t> struct c {}; template <class t> struct c<t, t(1)>; // notice value initialization
so partially specialized non type argument t t
involves template parameter of partial specialization class t
in expression other identifier; furthermore such specializations bound involve class t
in value initialization violation of rules. c3 comes along , clears out won't have make deduction every time.
so far we've established rules in sync not prove case 2 (once remove initial limitation every other related limitation falls apart). we'd have dive matching class template partial specializations rules; partial ordering considered out of scope here because if can produce valid candidates it's programmer put formed program (i.e. not create ambiguous uses of class templates).
section
matching of class template partial specializations [temp.class.spec.match]
describes (give or take) "pattern matching" process involved in template specialization. rule 1
overall workflow of procedure , subsequent rules define correctness
a partial specialization matches given actual template argument list if template arguments of partial specialization can deduced actual template argument list
a non-type template argument can deduced value of actual template argument of non-type parameter of primary template.
in type name refers class template specialization, (e.g., a) argument list shall match template parameter list of primary template. template arguments of specialization deduced arguments of primary template.
these rules not violated allowing the type of template parameter corresponding specialized non-type argument dependent on parameter of specialization. imho there no specific reason why can't have feature in future revisions of language: legacy blame. sadly didn't manage find language proposals initiative introduce feature.
Comments
Post a Comment