C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Defect report: not possible to declare-but-not-define a full specialization of a variable template

From: Brian Bi <bbi5291_at_[hidden]>
Date: Tue, 14 Jan 2025 10:22:07 -0500
 I'll try to summarize some key points gleaned from the reflector
discussion.

The `extern` keyword is not allowed on an explicit specialization of a
variable template ([temp.expl.spec]/2). However, it's allowed for partial
specializations and no one can remember the rationale for the difference.
We could probably just allow `extern`, but there's a catch.

There is an asymmetry between how static data members and namespace-scope
variables are treated. For static data members, it is possible to have a
non-defining declaration of an explicit specialization. The rule is that if
an initializer is present, it's considered a definition, otherwise it's
considered a non-defining declaration. [temp.expl.spec]/13

It would be desirable to consider whether we could somehow harmonize the
treatment of the two*. *The problem is that, ideally, static data members
should follow the same rule as other variables, i.e., it's a definition if
`extern` isn't present. Meaning that, if there's no initializer, the
meaning would change from "this is a non-defining declaration" to "this is
a definition that specifies default initialization". That's a problem.

It was suggested to explore the possibility of a new syntax where `extern`
goes before the `template <>` (similar to an explicit instantiation
declaration). Quoting from a message:

If we want this to apply to static data members as well as namespace-scope
> variable templates, we'll need a long transition path to go from
>
> template<> long ct<long>::dm; // not a definition
> extern template<> long ct<long>::dm; // ill-formed
>
> to
>
> template<> long ct<long>::dm; // deprecated
> extern template<> long ct<long>::dm; // not a definition
>
> to eventually
>
> template<> long ct<long>::dm; // definition
> extern template<> long ct<long>::dm; // not a definition


On Tue, Jan 14, 2025 at 10:10 AM Christof Meerwald via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Tue, Jan 14, 2025 at 09:55:00AM -0500, Arthur O'Dwyer via Std-Proposals
> wrote:
> > Maybe you guys missed it, but Brian Bi already gave the authoritative
> > answer in this thread: "This is a known issue and needs a paper to fix."
> > (Brian, is there any prior discussion — a CWG issue, any old papers,
> etc.?)
>
> There was a thread on the core mailing list: "Can an explicit
> specialization of a variable template be forward declared?" in July
> 2024. Even has a volunteer to write a paper.
>

Considering that a paper hasn't yet materialized, I wouldn't let the fact
that someone volunteered to write one dissuade anyone else from putting in
the work, if they care about this.


>
>
> Christof
>
> --
> https://cmeerw.org sip:cmeerw at cmeerw.org
> mailto:cmeerw at cmeerw.org xmpp:cmeerw at cmeerw.org
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>


-- 
*Brian Bi*

Received on 2025-01-14 15:22:22