andreasfertig / cppinsights

C++ Insights - See your source code with the eyes of a compiler
https://cppinsights.io
MIT License
4.11k stars 243 forks source link

There is a difference between cpp insight and C++ Templates #472

Closed Ladonnaemobile closed 2 years ago

Ladonnaemobile commented 2 years ago

code

https://cppinsights.io/lnk?code=Y2xhc3MgTXlJbnQgewpwdWJsaWM6CglNeUludChpbnQpOwp9OwpNeUludCBvcGVyYXRvci0oTXlJbnQgY29uc3QgJik7CmJvb2wgb3BlcmF0b3I+KE15SW50IGNvbnN0ICYsIE15SW50IGNvbnN0ICYpOwp1c2luZyBJbnQgPSBNeUludDsKCnRlbXBsYXRlIDx0eXBlbmFtZSBUPgp2b2lkIGYoVCB0KSB7CglpZiAodCA+IDApIHsKCQlnKC10KTsKCX0KfQp2b2lkIGcoSW50KSB7CglmPEludD4oNDIpOwp9&insightsOptions=cpp20&std=cpp20&rev=1.0

C++ Templates says the POI(Points of Instantiation) should be #_4, but the cpp insight inserted the code before the function g (namely #_1).

andreasfertig commented 2 years ago

Hello @Ladonnaemobile,

thanks for reporting this! Do you have an exact reference to C++ Templates? I assume it is the book you're talking about. But the chapter / page would be great.

The reference aside, placing the instantiation directly below the primary template is the easiest implementation. More interesting, if I move the transformed code in the order you suggest, Clang and GCC complain (link)

<source>:32:6: error: explicit specialization of 'f<MyInt>' after instantiation
void f<MyInt>(MyInt t)
     ^
<source>:26:3: note: implicit instantiation first required here
  f<Int>(MyInt(42));
  ^
1 error generated.
Compiler returned: 1

That makes me wonder about the reference.

Andreas

Ladonnaemobile commented 2 years ago

Hello @Ladonnaemobile,

thanks for reporting this! Do you have an exact reference to C++ Templates? I assume it is the book you're talking about. But the chapter / page would be great.

The reference aside, placing the instantiation directly below the primary template is the easiest implementation. More interesting, if I move the transformed code in the order you suggest, Clang and GCC complain (link)

<source>:32:6: error: explicit specialization of 'f<MyInt>' after instantiation
void f<MyInt>(MyInt t)
     ^
<source>:26:3: note: implicit instantiation first required here
  f<Int>(MyInt(42));
  ^
1 error generated.
Compiler returned: 1

That makes me wonder about the reference.

Andreas

Thanks for your notice. The book is C++ Templates 2nd Edition and the related content is in Chapter14(Instantiation),14.3.2(Points of Instantiation).
The POI is confusing, maybe I mistook something....

Ladonnaemobile commented 2 years ago

Hello @Ladonnaemobile, thanks for reporting this! Do you have an exact reference to C++ Templates? I assume it is the book you're talking about. But the chapter / page would be great. The reference aside, placing the instantiation directly below the primary template is the easiest implementation. More interesting, if I move the transformed code in the order you suggest, Clang and GCC complain (link)

<source>:32:6: error: explicit specialization of 'f<MyInt>' after instantiation
void f<MyInt>(MyInt t)
     ^
<source>:26:3: note: implicit instantiation first required here
  f<Int>(MyInt(42));
  ^
1 error generated.
Compiler returned: 1

That makes me wonder about the reference. Andreas

Thanks for your notice. The book is C++ Templates 2nd Edition and the related content is in Chapter14(Instantiation),14.3.2(Points of Instantiation). The POI is confusing, maybe I mistook something....

https://godbolt.org/z/8Pc785jWo As the book says, if place the instantiation above function g, g is not visible.

andreasfertig commented 2 years ago

Hello @Ladonnaemobile,

thanks for the clarification. I assume the book refers to what the standard specifies in temp.point.

I agree with your finding, Unfortunately. I don't see an easy way to implement a fix. However, I will create a page that lists the limitations of C++ Insights. Starting with this one.

Andreas