问题
Simplified:
// 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
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)
回答1:
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...
}
回答2:
One idea that works is to remove the pointer and simply have T
instead, with a std::enable_if_t<std::is_pointer<T>::value>
guard. Simplified example below:
#include <iostream>
#include <type_traits>
template<class T, size_t N>
void f(T const (&) [N])
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
template<class T, std::enable_if_t<std::is_pointer<T>::value>* = nullptr >
void f(T)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
int main()
{
const char* str = "test";
char str2[]{"test2"};
f(str);
f(str2);
}
Live on Coliru
来源:https://stackoverflow.com/questions/41290958/how-to-avoid-decay-with-template-parameter-deduction