【发布时间】:2020-09-15 10:10:47
【问题描述】:
我有一个遍历函数,它为每个元素调用回调,并希望在编译时使用非默认参数实现它的逻辑扩展,如下所示:
#include <type_traits>
#include <iostream>
using namespace std;
template<
typename Odds = void,
typename Evens = void,
typename Skip = void
>
void iterate(
const auto & on_element,
const auto & skip
) {
for ( int i = 0; i < 6; ++ i ) {
if constexpr ( ! is_same_v< Skip, void > ) {
if ( skip.find( i ) != skip.end() )
continue;
}
if ( i % 2 == 1 ) {
if constexpr ( is_same_v< Odds, void > )
on_element( i );
}
else {
if constexpr ( is_same_v< Evens, void > )
on_element( i );
}
static_assert(
is_same_v< Odds, void > || is_same_v< Evens, void >,
"Either odds or evens are required to get traversed"
);
}
}
int main() {
const auto & on_element = []( const int & i ) {
cout << " " << i << endl;
};
cout << "Whole range:" << endl;
iterate( on_element );
cout << "Should skip specified elements:" << endl;
iterate( on_element, { 1, 2, 3 } );
cout << "Should traverse only evens:" << endl;
iterate< bool >( on_element );
cout << "Should traverse only odds:" << endl;
iterate< void, bool >( on_element );
//should NOT compile if uncomment:
//iterate< bool, bool >( on_element );
}
但它不能用g++ -std=gnu++2a -fconcepts main.cpp -o main编译:
main.cpp: In function ‘int main()’:
main.cpp:42:25: error: no matching function for call to ‘iterate(const main()::<lambda(const int&)>&)’
42 | iterate( on_element );
| ^
main.cpp:10:6: note: candidate: ‘template<class Odds, class Evens, class Skip, class auto:11, class auto:12> void iterate(const auto:11&, const auto:12&)’
10 | void iterate(
| ^~~~~~~
main.cpp:10:6: note: template argument deduction/substitution failed:
main.cpp:42:25: note: candidate expects 2 arguments, 1 provided
42 | iterate( on_element );
| ^
main.cpp:45:38: error: no matching function for call to ‘iterate(const main()::<lambda(const int&)>&, <brace-enclosed initializer list>)’
45 | iterate( on_element, { 1, 2, 3 } );
| ^
main.cpp:10:6: note: candidate: ‘template<class Odds, class Evens, class Skip, class auto:11, class auto:12> void iterate(const auto:11&, const auto:12&)’
10 | void iterate(
| ^~~~~~~
main.cpp:10:6: note: template argument deduction/substitution failed:
main.cpp:45:38: note: couldn’t deduce template parameter ‘auto:12’
45 | iterate( on_element, { 1, 2, 3 } );
| ^
main.cpp:48:33: error: no matching function for call to ‘iterate<bool>(const main()::<lambda(const int&)>&)’
48 | iterate< bool >( on_element );
| ^
main.cpp:10:6: note: candidate: ‘template<class Odds, class Evens, class Skip, class auto:11, class auto:12> void iterate(const auto:11&, const auto:12&)’
10 | void iterate(
| ^~~~~~~
main.cpp:10:6: note: template argument deduction/substitution failed:
main.cpp:48:33: note: candidate expects 2 arguments, 1 provided
48 | iterate< bool >( on_element );
| ^
main.cpp:51:39: error: no matching function for call to ‘iterate<void, bool>(const main()::<lambda(const int&)>&)’
51 | iterate< void, bool >( on_element );
| ^
main.cpp:10:6: note: candidate: ‘template<class Odds, class Evens, class Skip, class auto:11, class auto:12> void iterate(const auto:11&, const auto:12&)’
10 | void iterate(
| ^~~~~~~
main.cpp:10:6: note: template argument deduction/substitution failed:
main.cpp:51:39: note: candidate expects 2 arguments, 1 provided
51 | iterate< void, bool >( on_element );
| ^
有没有办法在 C++ 中实现它? 也许有更好的编译时技术来实现所需的行为?
【问题讨论】:
-
由于除了针对
void进行测试之外,您没有将模板类型用于任何其他用途,因此您应该考虑使用bool模板参数。 -
@IlCapitano 我想我在精神上坚持
auto太多,无法弄清楚如何检查参数是否已通过^^,但确实是bool T = true。感谢您指出这一点!
标签: c++ templates c++20 template-argument-deduction