[英]C++ specialized template function receiving literal string
I am writting a template method which has many specializations.我正在编写一个具有许多专业化的模板方法。
class FieldValue
{
public:
...
template< typename T >
void Set( const T& value );
...
};
One of them is:其中之一是:
template <>
void FieldValue::Set< const char* >( const char* const& value )
{
...
}
But when I try to call但是当我尝试打电话时
FieldValue fieldValue;
fieldValue.Set( "literal string" );
it expects an specialization for const char[14]
which is the length of "literal string"
, not a const char*
它期望const char[14]
的专业化,这是"literal string"
的长度,而不是const char*
Is there any work around this without the need to cast it to const char*
?有没有解决这个问题而不需要将它转换为const char*
?
UPDATE更新
After Rakete1111's recommendation, that's what I did:在 Rakete1111 的推荐之后,这就是我所做的:
class FieldValue
{
public:
...
template< typename T >
void Set( const T& value );
template< std::size_t N >
void Set( const char( &value )[ N ] )
{
Set( static_cast< const char* >( value ) );
}
...
};
This will end up calling the const char*
specialization这将最终调用const char*
专业化
If you were asking this for a class, I would have answered that you can use partial specialization:如果你在上课时问这个,我会回答你可以使用部分专业化:
template<std::size_t N>
struct Foo<const char (&)[N]> {};
But that doesn't work for function templates.但这不适用于函数模板。 Instead, you can overload them:相反,您可以重载它们:
template<std::size_t N>
void FieldValue::Set(const char (&Value)[N]) {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.