首页 > 解决方案 > 如何将 void 模板类型用于编译时条件代码和 static_assert?

问题描述

我有遍历函数,它为每个元素调用回调,并希望在编译时使用非默认参数实现它的逻辑扩展,如下所示:

#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++ 中实现它?也许有更好的编译时技术来实现所需的行为?

标签: c++templatesc++20template-argument-deduction

解决方案


template<
  bool Odds = false,
  bool Evens = false,
  bool Skip = false // or really, true: why pass skip if we won't use it?
>
void iterate(
  const auto & on_element,
  const auto & skip
)

并编写另一个重载:

template<
  bool Odds = false,
  bool Evens = false
> // no bool Skip; lack of arg is enough
void iterate(
  const auto & on_element
)
{
  iterate<Odds, Evens, false>(on_element);
}

然后替换! is_same_v< Skip, void >Skip和类似的赔率/偶数。

static_assert( !Skip||!std::is_same_v<decltype(skip), const int&>, "You must pass a 2nd argument" );

对生活质量有好处。


推荐阅读