[clang] [Clang][Sema] Diagnose variable template explicit specializations with storage-class-specifiers (PR #93873)

David Blaikie via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 2 12:10:10 PDT 2024


dwblaikie wrote:

> It looks like the presence of `static` on template variable specializations makes difference in the namespace context: https://gcc.godbolt.org/z/WGsreqbz8
> 
> Specifically, the specializations not marked `static` result in an exported variable. Thus, we have seemingly valid code that generates this diagnostic:
> 
> ```
> namespace A {
> template <unsigned N>
> static constexpr unsigned kMaxUnsignedInt = 2 * kMaxUnsignedInt<N - 1> + 1;
> 
> template <>
> static constexpr unsigned kMaxUnsignedInt<1> = 1;
> }
> ```
> 
> However, if we remove the `static` from the specialization, we're getting a potential ODR violation (considering this code can be in a header included into multiple translation units).
> 
> The right thing for this specific case is `inline constexpr`, but nevertheless, there seems to be an inconsistency now in how Clang handles the case now.

This seems to be pretty clearly a bug - in clang's specialization handling even prior to this patch.
GCC doesn't do this, it uses the storage class of the generic template for the specialization, which seems correct to me.

@sdkrystian are you interested in/able to fix this ^ ? https://gcc.godbolt.org/z/oPxKvvdeM


https://github.com/llvm/llvm-project/pull/93873


More information about the cfe-commits mailing list