c++ - 当给定 const char * 作为模板化参数的类型时,为什么编译器会选择 bool 而不是 string_view?
问题描述
#include <iostream>
struct A
{
void update(bool const & v)
{
std::cout << std::boolalpha << v << std::endl;
}
void update(std::string_view v)
{
std::cout << v << std::endl;
}
};
template <typename T>
void update(T const & item)
{
A a;
a.update(item);
}
int main()
{
const char * i = "string";
update(i);
}
当我用 a 调用 update 时const char *
,编译器用bool
参数而不是string_view
?! 为什么 ??!
解决方案
从const char *
to的转换std::string_view
(通过 的构造函数std::string_view
)是用户定义的转换;这比重载决议中的标准转换(从to的隐式转换)更差。const char*
bool
1) 标准转换序列总是优于用户定义的转换序列或省略号转换序列。
推荐阅读
- python - 程序没有返回值
- c - 无法弄清楚如何通过 USB 将数据发送到从 stm32f103c8 到 PC 的帽子开关
- c# - C# 使用反射仅列出顶级类,不包括链接的类
- node.js - docker-compose up 无法构建 Gatsby app npm 问题
- python-3.x - 如何绘制分类与分类图?
- python - python plotnine:如何更改颜色比例
- graphql - GraphQL Playground 不断强制服务器的上下文函数运行?
- node.js - EADDRINUSE:地址已在使用 :::5000,每次我运行我的节点/反应应用程序时都会发生
- sql-server - 如何使用 T-SQL 分配永久唯一 ID
- python - 循环遍历二维数组,如果值为真,则追加到一个空数组中