Issue 109897
Summary [libc++] self-defined function named 'print' may cause failure in overload resolution
Labels libc++
Assignees
Reporter Yuzhiy05
    The following function will result in overload resolution failure
```c++
#include <iostream>
#include <unordered_map>
void print(auto const comment, auto const& map)
{
    std::cout << comment << "{";
    for (const auto &pair : map)
        std::cout << "{" << pair.first << ": " << pair.second << "}";
    std::cout << "}\n";
}
 
int main()
{
    std::unordered_map<char, int> map{{'a', 27}, {'b', 3}, {'c', 1}};
 
    print("map", map);
 
}
```
result
```c++
<source>:16:5: error: call to 'print' is ambiguous
   16 |     print("map", map);
      | ^~~~~
<source>:4:6: note: candidate function [with comment:auto = const char *, map:auto = std::unordered_map<char, int>]
    4 | void print(auto const comment, auto const& map)
      | ^
/opt/compiler-explorer/clang-trunk-20240924/bin/../include/c++/v1/print:341:28: note: candidate function [with _Args = <std::unordered_map<char, int> &>]
 341 | _LIBCPP_HIDE_FROM_ABI void print(format_string<_Args...> __fmt, _Args&&... __args) {
      |                            ^
1 error generated.
```
options
```
-std=c++26 -stdlib=libc++
```
[godbolt](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:15,endLineNumber:16,positionColumn:15,positionLineNumber:16,selectionStartColumn:15,selectionStartLineNumber:16,startColumn:15,startLineNumber:16),source:'%23include+%3Ciostream%3E%0A%23include+%3Cunordered_map%3E%0A+%0Avoid+print(auto+const+comment,+auto+const%26+map)%0A%7B%0A++++std::cout+%3C%3C+comment+%3C%3C+%22%7B%22%3B%0A++++for+(const+auto+%26pair+:+map)%0A++++++++std::cout+%3C%3C+%22%7B%22+%3C%3C+pair.first+%3C%3C+%22:+%22+%3C%3C+pair.second+%3C%3C+%22%7D%22%3B%0A++++std::cout+%3C%3C+%22%7D%5Cn%22%3B%0A%7D%0A+%0Aint+main()%0A%7B%0A++++std::unordered_map%3Cchar,+int%3E+map%7B%7B!'a!',+27%7D,+%7B!'b!',+3%7D,+%7B!'c!',+1%7D%7D%3B%0A+%0A++++print(%22map%22,+map)%3B%0A+%0A%7D'),l:'5',n:'1',o:'C%2B%2B+source+%231',t:'0')),k:49.259743087306774,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:executor,i:(argsPanelShown:'1',compilationPanelShown:'0',compiler:clang_trunk,compilerName:'',compilerOutShown:'0',execArgs:'',execStdin:'',fontScale:14,fontUsePx:'0',j:1,lang:c%2B%2B,libs:!(),options:'-std%3Dc%2B%2B26+++-stdlib%3Dlibc%2B%2B',overrides:!(),runtimeTools:!(),source:1,stdinPanelShown:'1',wrap:'1'),l:'5',n:'0',o:'Executor+x86-64+clang+(trunk)+(C%2B%2B,+Editor+%231)',t:'0')),header:(),k:50.740256912693226,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

The C++ standard documentation requires that <ostream> support the inclusion of a std::print(std::ostream&...) , However, libc++  instead includes a complete <print> header , such as std::print(format_string<_Args...> __fmt, _Args&&... __args), then ADL might cause confusion

I do not want to  include the header  <print>, but including <iostream> might implicitly involve it
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to