c++ - 涉及自动参数和临时对象的模板专业化选择问题
问题描述
给定以下代码,具有自动参数的模板函数的特化......
#include <iostream>
#include <string>
template<typename T>
void f(auto val);
template<>
void f<int>(const std::string&) { std::cout << "ok\n"; }
int main() {
f<int, const std::string&>("xxx"); // ok
// f<int>("xxx"); // won't compile
f<int, const std::string&>(std::string("xxx")); // ok
// f<int>(std::string("xxx")); // won't compile
}
为什么编译器(在这种情况下是带有 C++17 的 GCC)在没有明确指定自动参数类型的情况下不能选择专业化?
解决方案
推荐阅读
- c++ - 转换 STL 列表
成向量 c++11 - swift - 在 Swift AwaitKit 中使用 Firestore 调用?
- javascript - AJAX 没有从 JS 调用 php 文件?
- scala - Scala 使用外部 val 生成时间序列数据
- php - symfony simple-phpunit 错误'您请求了一个不存在的服务“test.client”'仅在部署脚本中
- mysql - 在另一列有效的基础上添加两列并重复使用
- javascript - 在正则表达式中使用 OR 用 JS replace() 替换多个匹配项
- python - Pyomo:AttributeError:'dict'对象没有属性'__default_objective__'
- logstash - 如何调试 Grok 解析失败
- html - 网站上出现此错误的原因可能是什么?