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 06:29:32 -0500
On Mon, 2025-01-13 at 13:39 -0800, Thiago Macieira via Std-Proposals
wrote:
> On Monday 13 January 2025 12:41:02 Pacific Standard Time Avi Kivity
> via Std-
> Proposals wrote:
> > Unfortunately, that doesn't work.
> >
> >
> > template <typename T>
> > struct A {
> > static int x;
> > };
>
> You need to declare that this template will be specialised later.
>
> extern template struct A<int>;
>


It does not help.

New source:

template <typename T>
struct A {
    static int x;
};

extern template struct A<int>;

int f() {
    return A<int>::x + A<long>::x;
}


template <>
int A<int>::x = 4;



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


https://godbolt.org/z/1WPodxq6n

Received on 2025-01-14 11:29:36