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: Avi Kivity <avi_at_[hidden]>
Date: Tue, 14 Jan 2025 12:38:45 -0500
On Tue, 2025-01-14 at 09:55 -0500, Arthur O'Dwyer 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.?)
>
> The natural syntax seems to be https://gcc.godbolt.org/z/6n86jnY4W
>
> template <typename T> int x;
> template <typename T> int f();
> template<> extern int x<int>;
> template<> extern int f<int>();
>


In fact this worked in clang 18, but no longer does in clang 19. I had
to revert to explicitly instantiated function templates:
https://github.com/scylladb/scylladb/pull/22299

> int f() {
> return x<int> + x<long>;
> }
>
> template <>
> int x<int> = 4;
>
> template <>
> int f<int>() { return 4; }
>
> Clang trunk already accepts this syntax, with a warning that it's
> non-standard.


Ah, maybe clang 18 supported it without a warning, and 19 just added
the warning (and we use -Werror). Still, I'd rather not rely on non-
standard extensions.

> GCC, EDG, and MSVC all reject: `template<> extern [...]` isn't
> allowed because the grammar for a template specialization doesn't
> include storage-class for some historical reason
> ([temp.expl.spec]/2).
>
> Seems like a good idea to me, FWIW; but going back and forth on this
> mailing list isn't going to change the status quo. "This is a known
> issue and needs a paper to fix."
>
> –Arthur
>
>
>
>
> On Tue, Jan 14, 2025 at 9:13 AM Thiago Macieira via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> > On Tuesday 14 January 2025 03:29:32 Pacific Standard Time Avi
> > Kivity wrote:
> > > Errors:
> > >
> > > <source>:14:13: error: explicit specialization of 'x' after
> > > instantiation
> > > 14 | int A<int>::x = 4;
> > >
> > > | ^
> > >
> > > <source>:6:24: note: explicit instantiation first required here
> > > 6 | extern template struct A<int>;
> > >
> > > | ^
> > >
> > > 1 error generated.
> > > Compiler returned: 1
> >
> > Works with three other compiler front-ends:
> > https://gcc.godbolt.org/z/ddTqrqn7f
> >
> > I don't know if this is a Clang bug or just under-specified in the
> > standard.
> >
> > --
> > Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> > Principal Engineer - Intel DCAI Platform & System Engineering
> >
> >
> >

Received on 2025-01-14 17:38:48