如何通过模板参数推导避免衰减 [英] How to avoid decay with template parameter deduction

查看:76
本文介绍了如何通过模板参数推导避免衰减的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简体:

// 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...
}

这篇关于如何通过模板参数推导避免衰减的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆