C++ Logo


Advanced search

Re: Allow use of constants and types from not-instantiation-ed template

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Mon, 20 Dec 2021 01:30:35 -0500
On Sun, Dec 19, 2021 at 2:36 PM Maciej Polanski via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> Hello,
> So, let's just allow use of a types and static constants from
> not-instantiation-ed template class (if possible):
> template<typename T>
> struct Foo{
> constexpr static int bar = 0x01;
> };
> int i = Foo::bar; // Doesn't compile now, but could
> int j = Foo<class Nonexisting>::bar; // Ugly hack but works
> This improvement should make template configuration behave similar to
> class usages.

But... they shouldn't.

Templates *aren't* classes, and we shouldn't pretend that they are.
Template argument deduction is a special case, but that's simply an
indirect mechanism to specify a template's parameters.

A template is a construct which, once supplied with parameters,
creates a class, function, or variable. `Foo` doesn't contain
anything, because a template is just a template. It doesn't become a
class until you give it template parameters.

Furthermore, if `bar` has no relation to the template parameter of
`Foo`, then there's a good argument to be made that it shouldn't be a
member at all. Making it a member allows explicit or partial
specializations of `Foo` to change this variable's value. If allowing
this is not the intent, then it ought not be a member. And if that
*is* the intent, then `Foo::bar` is nonsensical, since `bar` has no
meaning outside of a specific instantiation of `Foo`, which can yield
a different result from the primary template.

Received on 2021-12-20 00:30:49