код:
#include <functional>
#include <type_traits>
namespace impl
{
// lame recursive implementation
template<typename Functor, Functor F, typename T, T OneValue, T SecondValue, T... Values>
struct variadic_conjunction : variadic_conjunction<Functor, F, T, F(OneValue, SecondValue), Values...> {};
template<typename Functor, Functor F, typename T, T OneValue, T SecondValue>
struct variadic_conjunction<Functor, F, T, OneValue, SecondValue> : std::integral_constant<T, F(OneValue, SecondValue)> {};
}
template<typename Functor, Functor F, typename T, T... Values>
struct variadic_conjunction : impl::variadic_conjunction<Functor, F, T, Values...> {};
template<typename Functor, Functor F, typename T, T... Values>
constexpr T variadic_conjunction_v = variadic_conjunction<Functor, F, T, Values...>::value;
template<typename T, T... Values>
struct variadic_max : variadic_conjunction<std::greater<T>, std::greater<T>{}, T, Values...> {};
template<typename T, T... Values>
constexpr T variadic_max_v = variadic_max<T, Values...>::value;
int main()
{
constexpr auto max = variadic_max_v<std::size_t, 4, 8>;
}
Вы можете играть с ним на coliru: http://coliru.stacked -crooked.com / a / 17c5065229594de9 .
На это жалуется:
main.cpp:14:36: error: a non-type template parameter cannot have type 'std::__1::greater<unsigned long>'
template<typename Functor, Functor F, typename T, T... Values>
^
main.cpp:21:23: note: while substituting prior template arguments into non-type template parameter 'F' [with Functor = std::__1::greater<unsigned long>]
struct variadic_max : variadic_conjunction<std::greater<T>, std::greater<T>{}, T, Values...> {};
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:24:30: note: in instantiation of template class 'variadic_max<unsigned long, 4, 8>' requested here
constexpr T variadic_max_v = variadic_max<T, Values...>::value;
^
main.cpp:28:26: note: in instantiation of variable template specialization 'variadic_max_v<unsigned long, 4, 8>' requested here
constexpr auto max = variadic_max_v<std::size_t, 4, 8>;
^
main.cpp:28:26: error: constexpr variable 'max' must be initialized by a constant expression
constexpr auto max = variadic_max_v<std::size_t, 4, 8>;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.
Но, насколько я могу судить, я передаю экземпляр std::greater<T>
(обратите внимание на инициализацию скобки {}
).
Я также не уверен насчет второй ошибки, но вернусь к ней позже.