C++ Logo


Advanced search

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

From: Tom Honermann <tom_at_[hidden]>
Date: Sun, 19 Dec 2021 18:38:51 -0500
On 12/19/21 2:35 PM, Maciej Polanski via Std-Proposals 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

The reason this isn't allowed is because, in the absence of a template
specialization, the presumed data member doesn't exist; even when
declared static, Foo<int>::bar and Foo<long>::bar are different data
members (even if not dependent). This is what allows such data members
to be specialized in a partial or explicit template specialization:

template<typename T>
struct Foo<T*> {
   constexpr static int bar = 0x02;
struct Foo<int> {
   constexpr static int bar = 0x03;

This doesn't preclude the possibility of adding a new form of (implicit)
instantiation in which the syntax you propose triggers instantiation of
an (obviously non-dependent) global variable of sorts (it wouldn't be a
static data member because there would be no associated class). That
would be rather novel and different from how template instantiation and
specialization works.


Received on 2021-12-19 17:38:54