首页 > 解决方案 > 无法将 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++visual-studio

解决方案


这里的罪魁祸首是我的构造函数中的移动语义,&&它不能接受 c 字符串。


推荐阅读