gpt4 book ai didi

c++ - 带有可变参数模板类的 SFINAE?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:24 27 4
gpt4 key购买 nike

我似乎找不到将 SFINAE 与可变模板类一起使用的好解决方案。

假设我有一个不喜欢引用的可变参数模板对象:

template<typename... Args>
class NoRef
{
//if any of Args... is a reference, this class will break
//for example:
std::tuple<std::unique_ptr<Args>...> uptrs;
};

还有一个方便地检查参数包是否包含引用的类:

template<typename T, typename... Other>
struct RefCheck
{
static const bool value = std::is_reference<T>::value || RefCheck<Other...>::value;
};
template<typename T>
struct RefCheck<T>
{
static const bool value = std::is_reference<T>::value;
};

对于 arg 包中存在引用的情况,我如何使用它来专门化 NoRef?

最佳答案

这不使用 SFINAE,但基本上按照您的意图进行:

template<bool Ref, typename... Args>
class NoRef_;

template<typename... Args>
class NoRef_<false, Args...>
{
std::tuple<std::unique_ptr<Args>...> uptrs;
};
template<typename... Args>
class NoRef_<true, Args...>
{
// contains reference
};

template<typename... Args>
using NoRef = NoRef_<RefCheck<Args...>::value, Args...>;

// alternative from Nawaz

template<typename... Args> struct NoRef : NoRef_<RefCheck<Args...>::value, Args...> {}

关于c++ - 带有可变参数模板类的 SFINAE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13948864/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com