是的,概念精简版基本上装扮了 SFINAE。此外,它允许更深入的内省以实现更好的超载。但是,这仅在概念谓词定义为时才有效concept bool
。改进的重载不适用于当前的概念谓词,但可以使用条件重载。让我们看看如何在 C++14 中定义谓词、约束模板和重载函数。这有点长,但它介绍了如何在 C++14 中创建完成此任务所需的所有工具。
定义谓词
首先,用 all thestd::declval
和decltype
到处来阅读谓词有点难看。相反,我们可以利用这样一个事实,即我们可以使用尾随 decltype(来自 Eric Niebler 的博客文章here)来约束函数,如下所示:
struct Incrementable
{
template<class T>
auto requires_(T&& x) -> decltype(++x);
};
因此,如果++x
无效,则该requires_
成员函数不可调用。因此,我们可以创建一个models
仅检查是否requires_
可调用的特征void_t
:
template<class Concept, class Enable=void>
struct models
: std::false_type
{};
template<class Concept, class... Ts>
struct models<Concept(Ts...), void_t<
decltype(std::declval<Concept>().requires_(std::declval<Ts>()...))
>>
: std::true_type
{};
约束模板
所以当我们想要基于概念约束模板时,我们仍然需要使用enable_if
,但是我们可以使用这个宏来帮助它变得更简洁:
#define REQUIRES(...) typename std::enable_if<(__VA_ARGS__), int>::type = 0
所以我们可以定义一个increment
基于Incrementable
概念约束的函数:
template<class T, REQUIRES(models<Incrementable(T)>())>
void increment(T& x)
{
++x;
}
因此,如果我们调用increment
不是 的东西Incrementable
,我们将收到如下错误:
test.cpp:23:5: error: no matching function for call to 'incrementable'
incrementable(f);
^~~~~~~~~~~~~
test.cpp:11:19: note: candidate template ignored: disabled by 'enable_if' [with T = foo]
template<class T, REQUIRES(models<Incrementable(T)>())>
^
重载函数
现在如果我们想做重载,我们想使用条件重载。假设我们想创建一个std::advance
使用概念谓词,我们可以这样定义它(现在我们将忽略可递减的情况):
struct Incrementable
{
template<class T>
auto requires_(T&& x) -> decltype(++x);
};
struct Advanceable
{
template<class T, class I>
auto requires_(T&& x, I&& i) -> decltype(x += i);
};
template<class Iterator, REQUIRES(models<Advanceable(Iterator, int)>())>
void advance(Iterator& it, int n)
{
it += n;
}
template<class Iterator, REQUIRES(models<Incrementable(Iterator)>())>
void advance(Iterator& it, int n)
{
while (n--) ++it;
}
concept bool
然而,当它与std::vector
迭代器一起使用时,这会导致模棱两可的重载(在精简概念中,这仍然是模棱两可的重载,除非我们将谓词更改为引用 a 中的其他谓词)。我们要做的是对调用进行排序,我们可以使用条件重载来做到这一点。可以考虑写这样的东西(这不是有效的C++):
template<class Iterator>
void advance(Iterator& it, int n) if (models<Advanceable(Iterator, int)>())
{
it += n;
}
else if (models<Incrementable(Iterator)>())
{
while (n--) ++it;
}
所以如果第一个函数没有被调用,它将调用下一个函数。因此,让我们首先为两个功能实现它。我们将创建一个名为的类basic_conditional
,它接受两个函数对象作为模板参数:
struct Callable
{
template<class F, class... Ts>
auto requires_(F&& f, Ts&&... xs) -> decltype(
f(std::forward<Ts>(xs)...)
);
};
template<class F1, class F2>
struct basic_conditional
{
// We don't need to use a requires clause here because the trailing
// `decltype` will constrain the template for us.
template<class... Ts>
auto operator()(Ts&&... xs) -> decltype(F1()(std::forward<Ts>(xs)...))
{
return F1()(std::forward<Ts>(xs)...);
}
// Here we add a requires clause to make this function callable only if
// `F1` is not callable.
template<class... Ts, REQUIRES(!models<Callable(F1, Ts&&...)>())>
auto operator()(Ts&&... xs) -> decltype(F2()(std::forward<Ts>(xs)...))
{
return F2()(std::forward<Ts>(xs)...);
}
};
所以现在这意味着我们需要将函数定义为函数对象:
struct advance_advanceable
{
template<class Iterator, REQUIRES(models<Advanceable(Iterator, int)>())>
void operator()(Iterator& it, int n) const
{
it += n;
}
};
struct advance_incrementable
{
template<class Iterator, REQUIRES(models<Incrementable(Iterator)>())>
void operator()(Iterator& it, int n) const
{
while (n--) ++it;
}
};
static conditional<advance_advanceable, advance_incrementable> advance = {};
所以现在如果我们尝试使用它std::vector
:
std::vector<int> v = { 1, 2, 3, 4, 5, 6 };
auto iterator = v.begin();
advance(iterator, 4);
std::cout << *iterator << std::endl;
它将编译并打印出来5
。
然而,std::advance
实际上有三个重载,所以我们可以使用basic_conditional
来实现conditional
,它适用于使用递归的任意数量的函数:
template<class F, class... Fs>
struct conditional : basic_conditional<F, conditional<Fs...>>
{};
template<class F>
struct conditional<F> : F
{};
所以,现在我们可以这样写std::advance
:
struct Incrementable
{
template<class T>
auto requires_(T&& x) -> decltype(++x);
};
struct Decrementable
{
template<class T>
auto requires_(T&& x) -> decltype(--x);
};
struct Advanceable
{
template<class T, class I>
auto requires_(T&& x, I&& i) -> decltype(x += i);
};
struct advance_advanceable
{
template<class Iterator, REQUIRES(models<Advanceable(Iterator, int)>())>
void operator()(Iterator& it, int n) const
{
it += n;
}
};
struct advance_decrementable
{
template<class Iterator, REQUIRES(models<Decrementable(Iterator)>())>
void operator()(Iterator& it, int n) const
{
if (n > 0) while (n--) ++it;
else
{
n *= -1;
while (n--) --it;
}
}
};
struct advance_incrementable
{
template<class Iterator, REQUIRES(models<Incrementable(Iterator)>())>
void operator()(Iterator& it, int n) const
{
while (n--) ++it;
}
};
static conditional<advance_advanceable, advance_decrementable, advance_incrementable> advance = {};
使用 Lambda 重载
但是,此外,我们可以使用 lambdas 来代替函数对象来编写它,这有助于使其编写起来更简洁。所以我们STATIC_LAMBDA
在编译时使用这个宏来构造 lambda:
struct wrapper_factor
{
template<class F>
constexpr wrapper<F> operator += (F*)
{
return {};
}
};
struct addr_add
{
template<class T>
friend typename std::remove_reference<T>::type *operator+(addr_add, T &&t)
{
return &t;
}
};
#define STATIC_LAMBDA wrapper_factor() += true ? nullptr : addr_add() + []
并添加一个make_conditional
函数constexpr
:
template<class... Fs>
constexpr conditional<Fs...> make_conditional(Fs...)
{
return {};
}
然后我们现在可以advance
像这样编写函数:
constexpr const advance = make_conditional(
STATIC_LAMBDA(auto& it, int n, REQUIRES(models<Advanceable(decltype(it), int)>()))
{
it += n;
},
STATIC_LAMBDA(auto& it, int n, REQUIRES(models<Decrementable(decltype(it))>()))
{
if (n > 0) while (n--) ++it;
else
{
n *= -1;
while (n--) --it;
}
},
STATIC_LAMBDA(auto& it, int n, REQUIRES(models<Incrementable(decltype(it))>()))
{
while (n--) ++it;
}
);
这比使用函数对象版本更紧凑和可读。
此外,我们可以定义一个modeled
函数来减少decltype
丑陋:
template<class Concept, class... Ts>
constexpr auto modeled(Ts&&...)
{
return models<Concept(Ts...)>();
}
constexpr const advance = make_conditional(
STATIC_LAMBDA(auto& it, int n, REQUIRES(modeled<Advanceable>(it, n)))
{
it += n;
},
STATIC_LAMBDA(auto& it, int n, REQUIRES(modeled<Decrementable>(it)))
{
if (n > 0) while (n--) ++it;
else
{
n *= -1;
while (n--) --it;
}
},
STATIC_LAMBDA(auto& it, int n, REQUIRES(modeled<Incrementable>(it)))
{
while (n--) ++it;
}
);
最后,如果您有兴趣使用现有的库解决方案(而不是像我展示的那样滚动您自己的解决方案)。Tick库提供了定义概念和约束模板的框架。Fit库可以处理函数和重载。