c++ - 无法将 const char 字符串传递给模板类
问题描述
所以我只是制作了一个 PoC 模板类来试验模板。
我想保持我的代码和以前一样,所以我决定简单地使用 C 风格的字符串而不是std::strings
.
我在 Visual Studio 2019 上使用 C++17 和微软编译器。
这里是:
template <typename T1, typename T2>
class Human
{
private:
T1 name;
T2 age;
public:
Human(T1&& name, T2&& age) : name(name), age(age) {}
};
但是,当我主要这样做时:
Human<const char*, int> brad("Brad", 21);
我收到一个错误:
no instance of constructor matches the argument list, argument types are: (const char [5], <int>)
。
我什至尝试过,Human<const char[5], int> brad("Brad", 21)
看看它是否会工作,但仍然是同样的错误。
有任何想法吗?
提前致谢。
解决方案
这里的罪魁祸首是我的构造函数中的移动语义,&&
它不能接受 c 字符串。
推荐阅读
- javascript - window.open with width and height 打开一个没有实际宽度和高度的窗口
- linux - Linux中不同的终端文件颜色是什么意思
- django - 如何链接 django 信号和 ajax
- javascript - 隐藏 Safari 浏览器内置图标
- javascript - JS 计算器错误
- maven - 面临 mvn 问题
- typo3 - 从 TYPO3 v9.5.27 迁移到 v10.4.21 后出错
- azure-devops - GitHub for Azure DevOps 中的依赖关系图是什么?
- html-email - 具有不断变化的边界的 Html 电子邮件和流动表格
- python - 如何让 Python 代码为 SUMO 运行多次?