sizeof with function template argument in declaration doesn't match the definition
Asked Answered
A

0

6

The following program

template<int> 
struct R{};

struct S
{
    template<typename T>
    auto f(T t) -> R<sizeof(t)>;
};

template<typename T>
auto S::f(T t) -> R<sizeof(t)> {}

compiles with Clang, but GCC 10.2 gives an error for the definition of f

error: no declaration matches 'R<sizeof (t)> S::f(T)'

This looks like a GCC bug, especially since GCC trunk currently gives an ICE

internal compiler error: canonical types differ for identical types 'R<sizeof (t)>' and 'R<sizeof (t)>'

Also, GCC compiles the program when the return type in both the declaration and the definition is changed to R<sizeof (T)>.

Is the program valid?

Here's the demo.

Alteration answered 10/4, 2021 at 21:38 Comment(3)
Well that's an interesting one. t should be within the name scope to be used in that sizeof expression. What's most interesting to me is that this fails even if you do something silly like R<sizeof(decltype(t))> (which I tried since gcc does handle decltype(t) correctly). I'm pretty sure this is a GCC bug.Sort
@Human-Compiler Yeah, it seems to have something to do with how gcc generates the "canonical" types when sizeof is involved. I'm definitely leaning towards it being a gcc bug.Alteration
Looks fixed in GCC trunk: gcc.godbolt.org/z/8vaMPG3eWUnni

© 2022 - 2024 — McMap. All rights reserved.