【发布时间】:2016-12-04 10:15:54
【问题描述】:
我正在尝试为std::string 参数创建一个专门的构造函数,但是当我使用字符串参数调用它时,总是使用另一个构造函数。
struct Literal : Expression
{
template <typename V>
Literal(V val)
{
value = val;
}
};
template <>
Literal::Literal(std::string const& val)
{
value = val.c_str();
}
无论是在类内定义还是类外定义都没有关系,或者像在发布的示例中一样,只在类外定义特化:当用std::string调用时,赋值value = val给出一个编译器错误。
我如何正确地为std::string 特化这个构造函数模板?
【问题讨论】:
-
你没有声明
value。这是您将得到的错误:main.cpp:12:9: error: 'value' was not declared in this scope value = val; -
@HenriqueBarcelos
ATL::CComVariant value在Expression中声明。为了简洁起见,我只发布了相关的模板内容。 -
我假设
value是在基类中定义的。
标签: c++ templates constructor template-specialization