Issue 174430
Summary __PRETTY_FUNCTION__ changes depending on template instantiation
Labels new issue
Assignees
Reporter bebuch
    ```cpp
template <auto V>
[[nodiscard]] constexpr
auto PrettyValueFn() {
    return __PRETTY_FUNCTION__;
}

template <typename>
struct TestTemplate {
 enum class TestScopedEnum { X, Y, Z };
};


#include <iostream>

int main() {
    // (void) TestTemplate<int>::TestScopedEnum::X; // Access value first and the output is as expected
    (void) PrettyValueFn<TestTemplate<int>::TestScopedEnum{}>(); // Changes __PRETTY_FUNCTION__ for given index
    std::cout << PrettyValueFn<TestTemplate<int>::TestScopedEnum::X>() << '\n';
 std::cout << PrettyValueFn<TestTemplate<int>::TestScopedEnum::Y>() << '\n';
    std::cout << PrettyValueFn<TestTemplate<int>::TestScopedEnum::Z>() << '\n';
}
```

Output:

```
auto PrettyValueFn() [V = (TestTemplate<int>::TestScopedEnum)0]
auto PrettyValueFn() [V = TestTemplate<int>::TestScopedEnum::Y]
auto PrettyValueFn() [V = TestTemplate<int>::TestScopedEnum::Z]
```

Expected output:

```
auto PrettyValueFn() [V = TestTemplate<int>::TestScopedEnum::X]
auto PrettyValueFn() [V = TestTemplate<int>::TestScopedEnum::Y]
auto PrettyValueFn() [V = TestTemplate<int>::TestScopedEnum::Z]
```

This happens if you use a scoped enum in a template.

It works as expected, if you use a non-scoped enum or if your scoped enum is not defined in a template.

[Compiler Explorer](https://gcc.godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:1,endLineNumber:21,positionColumn:1,positionLineNumber:21,selectionStartColumn:1,selectionStartLineNumber:21,startColumn:1,startLineNumber:21),source:'template+%3Cauto+V%3E%0A%5B%5Bnodiscard%5D%5D+constexpr%0Aauto+PrettyValueFn()+%7B%0A++++return+__PRETTY_FUNCTION__%3B%0A%7D%0A%0Atemplate+%3Ctypename%3E%0Astruct+TestTemplate+%7B%0A++++enum+class+TestScopedEnum+%7B+X,+Y,+Z+%7D%3B%0A%7D%3B%0A%0A%0A%23include+%3Ciostream%3E%0A%0Aint+main()+%7B%0A++++(void)+PrettyValueFn%3Cstatic_cast%3CTestTemplate%3Cint%3E::TestScopedEnum%3E(0)%3E()%3B+//+Changes+__PRETTY_FUNCTION__+for+given+index%0A++++std::cout+%3C%3C+PrettyValueFn%3CTestTemplate%3Cint%3E::TestScopedEnum::X%3E()+%3C%3C+!'%5Cn!'%3B%0A++++std::cout+%3C%3C+PrettyValueFn%3CTestTemplate%3Cint%3E::TestScopedEnum::Y%3E()+%3C%3C+!'%5Cn!'%3B%0A++++std::cout+%3C%3C+PrettyValueFn%3CTestTemplate%3Cint%3E::TestScopedEnum::Z%3E()+%3C%3C+!'%5Cn!'%3B%0A%7D%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:33.333333333333336,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang2110,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-std%3Dc%2B%2B17',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+21.1.0+(Editor+%231)',t:'0')),k:33.333333333333336,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compilerName:'x86-64+clang+21.1.0',editorid:1,fontScale:14,fontUsePx:'0',j:1,wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+clang+21.1.0+(Compiler+%231)',t:'0')),k:33.33333333333333,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

All versions of Clang up to the current HEAD are affected.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to