我努力做到这一点。我想创建重载的模板函数地狱,使此类调用成为可能并正确(GMock):
ASSERT_EQ(min(1, 2), 1);
ASSERT_EQ(min(std::less<>(),3,2), 2);
auto abs_comp = [](auto el1, auto el2){
return std::abs(el1) < std::abs(el2);
};
ASSERT_EQ(min(abs_comp, -1, -5), -1);
ASSERT_EQ(min(4, 3, 2, 1), 1);
除了这个断言之外,其他所有东西都很好:
ASSERT_EQ(min(std::less<>(), 3,2,1), 2);
当我提取函数本身以获得有意义的错误时:
min(std::less<>(), 3,2,1)
我明白了:
In file included from /home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/container_minimum_test.cpp:4:
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/../../src/lang/container_minimum.h: In instantiation of ‘First cppchallenge::lang::min(First, Args ...) [with First = int; Args = {}; <template-parameter-1-3> = std::enable_if<true, void>]’:
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/../../src/lang/container_minimum.h:17:30: required from ‘First cppchallenge::lang::min(First, Args ...) [with First = std::less<void>; Args = {int}; <template-parameter-1-3> = std::enable_if<false, void>]’
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/../../src/lang/container_minimum.h:17:19: required from ‘First cppchallenge::lang::min(First, Args ...) [with First = std::less<void>; Args = {int, int, int}; <template-parameter-1-3> = std::enable_if<false, void>]’
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/container_minimum_test.cpp:40:33: required from here
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/../../src/lang/container_minimum.h:17:30: error: no matching function for call to ‘min()’
return min(first, min(args...));
~~~^~~~~~~~~
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/../../src/lang/container_minimum.h:8:7: note: candidate: ‘template<class T> T cppchallenge::lang::min(T, T)’
T min(T first, T second) {
^~~
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/../../src/lang/container_minimum.h:8:7: note: template argument deduction/substitution failed:
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/../../src/lang/container_minimum.h:17:30: note: candidate expects 2 arguments, 0 provided
return min(first, min(args...));
~~~^~~~~~~~~
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/../../src/lang/container_minimum.h:16:11: note: candidate: ‘template<class First, class ... Args, class> First cppchallenge::lang::min(First, Args ...)’
First min(First first, Args... args) {
^~~
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/../../src/lang/container_minimum.h:16:11: note: template argument deduction/substitution failed:
/home/rumcajs/CLionProjects/ModernCppChallenge/tst/lang/../../src/lang/container_minimum.h:17:30: note: candidate expects at least 1 argument, 0 provided
return min(first, min(args...));
~~~^~~~~~~~~
gmake[3]: *** [CMakeFiles/ModernCppChallengeLang.dir/build.make:102: CMakeFiles/ModernCppChallengeLang.dir/tst/lang/container_minimum_test.cpp.o] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:116: CMakeFiles/ModernCppChallengeLang.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:128: CMakeFiles/ModernCppChallengeLang.dir/rule] Error 2
gmake: *** [Makefile:177: ModernCppChallengeLang] Error 2
模板功能如下:
namespace cppchallenge::lang {
//#1
template<typename T>
T min(T first, T second) {
return first < second ? first : second;
}
template<typename First, typename... Args>
using are_same = std::conjunction<std::is_same<First, Args>...>;
//#2
template<typename First, typename... Args, typename = std::enable_if<are_same<First, Args...>::value, void>>
First min(First first, Args... args) {
return min(first, min(args...));
}
//#3
template<typename Comparator, typename T>
T min(Comparator comp, T first, T second) {
return comp(first, second) ? first : second;
}
//#4
template<typename Comparator, typename First, typename... Args,
typename = std::enable_if<are_same<First, Args...>::value, void>,
typename std::enable_if<std::is_convertible<Comparator, std::function<bool(First,First)>>::value>::type>
First min(Comparator comp, First first, Args... args) {
return min(comp, first, min(comp, args...));
}
}
该错误指向功能#2,尽管它应使用#4。
答案 0 :(得分:2)
我想错误在于以下模板函数:您必须在最后一个int* &&
之后添加* = nullptr
作为
::type
或在最后一个template<typename Comparator, typename First, typename... Args,
typename = std::enable_if<are_same<First, Args...>::value, void>,
typename std::enable_if<std::is_convertible<Comparator, std::function<bool(First,First)>>::value>::type * = nullptr> // add * = nullptr
First min(Comparator comp, First first, Args... args) {
return comp(comp, first, min(comp, args...));
}
之前添加一个typename =
。
否则,如果一切顺利(如果typename std::enable_if
和所有Firts
相等,并且如果Args...
可转换为所需的Comparable
,则模板签名变为
st::function
最后一个template <typename Comparator, typename First, typename ... Args,
typename = std::enable_if<are_same<First, Args...>::value,
void>
单独是没有意义的(并且前面的void
没什么用;但这是另一个问题;请参阅以下“奖励建议”)>
您应该进行类似于
的转换std::enable_if
或者也是
template <typename Comparator, typename First, typename ... Args,
typename = std::enable_if<are_same<First, Args...>::value, void>
void * = nullptr>
//............^^^^^^^^^^^^
奖金建议:先前的SFINAE测试应该是
template <typename Comparator, typename First, typename ... Args,
typename = std::enable_if<are_same<First, Args...>::value, void>,
typename = void>
//........^^^^^^^^^^^
或(最后一个typename = std::enable_if_t<are_same<First, Args...>::value, void>
// ......................^^
参数默认为std::enable_if_t
)
void
否则,测试将永远无法进行,并且功能将永远处于启用状态(从typename = std::enable_if_t<are_same<First, Args...>::value>
和First
类型的角度来看)。
SFINAE测试中的类似问题
Args...