这被 gcc (live on godbolt) 都拒绝了和 clang :
#include <string>
namespace
{
std::string
to_string(char const (&str) [14])
{ return str; }
}
void f()
{
using std::to_string;
char const hello[14] = "Hello, World!";
(void) to_string(hello);
}
编译器会考虑 std::to_string
overloads 中的每一个并得出结论:
error: no matching function for call to 'to_string(const char [14])'
如果我删除 using std::to_string
,我的过载被考虑并调用。为什么?如何修复它(除了删除使用之外)?
最佳答案
using 声明的范围很重要。这是一个正确的声明,因此名称隐藏生效。在函数内部,全局范围版本不可见。您需要重新引入它:
using ::to_string;
using std::to_string;
关于c++ - 尽管看起来更匹配,但未考虑过载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58427809/