gpt4 book ai didi

c++ - 有没有一种方法可以基于当前类中的可用重载来进行 SFINAE?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:22:09 24 4
gpt4 key购买 nike

我已经使用这样的代码有一段时间了(至少从 GCC 4.9/Clang 3.5 开始):

#include <utility>

class foo
{
public:
void bar(int n);

template <typename R,
typename = decltype(std::declval<foo>().bar(*std::begin(std::declval<R>())))>
void bar(const R& range);
};

第二点bar()除非R,否则它应该被 SFINAE 移除是一个范围类型,其中重载了 bar()为其元素而存在。所以std::vector<int>会很好但是std::vector<int*>例如,不会。

不幸的是,从 Clang 3.9 开始,出现了这个错误:

templ.cpp:12:54: error: member access into incomplete type 'foo'
typename = decltype(std::declval<foo>().bar(*std::begin(std::declval<R>())))>
^
templ.cpp:6:7: note: definition of 'foo' is not complete until the closing '}'
class foo
^
1 error generated.

有没有一种方法可以不依赖于使用其自身定义中的不完整类型来完成此任务?

最佳答案

也许你可以让 foo 成为附加模板参数的默认值:

#include <utility>

class foo
{
public:
void bar(int n);

template <typename R,
typename F = foo,
typename = decltype(std::declval<F>().bar(*std::begin(std::declval<R>())))>
void bar(const R& range);
};

[live demo]

这会延迟检查 foo 是否完成。

关于c++ - 有没有一种方法可以基于当前类中的可用重载来进行 SFINAE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40595128/

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