如何通过模板参数推导避免衰减
问题描述:
简体:
// CHAR_TYPE == char, wchar_t, ...
template <typename CHAR_TYPE, unsigned CHAR_COUNT>
void Foo(CHAR_TYPE const (&value)[CHAR_COUNT]) noexcept
{
TRACE("const ref array");
// perform a bit of logic and forward...
}
template <typename CHAR_TYPE>
void Foo(CHAR_TYPE const* value) noexcept
{
TRACE("const ptr");
// perform a bit of logic and forward...
}
// [ several other overloads ]
Callsite:
char const* ptr = ...
wchar_t const* wptr = ...
Foo(ptr); // <-- good: "const ptr"
Foo(wptr); // <-- good: "const ptr"
constexpr char const buffer[] { "blah blah blah" };
constexpr wchar_t const wbuffer[] { L"blah blah blah" };
Foo(buffer); // <-- ambiguous
Foo(wbuffer); // <-- ambiguous
当然,我可以删除const ref数组重载。但是,我想以不同的方式处理这些类型。我试图有条件地启用正确的重载,但是我无法确定必要的条件。
Of course, I could remove the const ref array overload. However I would like to handle these types differently. I have tried to conditionally enable the correct overload, but I have not been able to determine the necessary condition.
template <typename CHAR_TYPE, unsigned COUNT>
typename std::enable_if</* std::is_?? */, void>::type
Foo(CHAR_TYPE const (&value)[COUNT]) noexcept
{
TRACE("by ref array");
// perform a bit of logic and forward...
}
template <typename CHAR_TYPE>
typename std::enable_if</* std::is_?? */, void>::type
Foo(CHAR_TYPE const* value) noexcept
{
TRACE("ptr");
// perform a bit of logic and forward...
}
消除这些重载的最佳方法是什么?
(我不希望使用数组包装器)
What is the best way to disambiguate these overloads?
(I would prefer not to use an array wrapper)
答
在模板参数推导过程中,通过(常量)引用获取参数可阻止数组到指针的衰减。参见 [temp.deduct.call] / 2 。因此:
Taking the argument by (const) reference blocks array-to-pointer decay during template argument deduction. See [temp.deduct.call]/2. So:
template <typename CHAR_TYPE>
void Foo(CHAR_TYPE const* const & value) noexcept
{
TRACE("const ptr");
// perform a bit of logic and forward...
}