使用 std::enable_if 时进行优化

Optimizing while using std::enable_if

考虑这段代码:

#include <iostream>
#include <type_traits>

template <std::size_t N> void bar() { std::cout << "bar<" << N << ">() called.\n"; }
template <std::size_t N> void hit() { std::cout << "hit<" << N << ">() called.\n"; }

template <typename T> struct evaluate : std::bool_constant<std::is_integral_v<T>> {
    static constexpr std::size_t size = sizeof(T);  // Simplified for illustration only.
};

void foo() { }

template <typename T, typename... Args>
std::enable_if_t<!evaluate<T>::value> foo (const T&, const Args&...);

template <typename T, typename... Args>
std::enable_if_t<evaluate<T>::value> foo (const T&, const Args&... args) {
    bar<evaluate<T>::size>();
    // Do whatever.
    foo(args...);
}

template <typename T, typename... Args>
std::enable_if_t<!evaluate<T>::value> foo (const T&, const Args&... args) {
    hit<evaluate<T>::size>();
    // Do whatever, but different from the previous foo overload.
    foo(args...);
}

int main() {
    foo (5, "hello", true);
}


Output:
bar<4>() called.
hit<6>() called.
bar<1>() called.

如何重写上面的内容,使得 evaluate<T> 每次 foo 迭代只需要计算一次而不是两次?

Ok, I thought evaluate was computed twice. But how would you make it appear only once (without using macros)? We are supposed to avoid repetition in code anyways

您可以尝试将其另存为默认值的附加模板参数

template <typename T, typename... Args, typename E = evaluate<T>>
std::enable_if_t<!E::value> foo (const T&, const Args&...);

template <typename T, typename... Args, typename E = evaluate<T>>
std::enable_if_t<E::value> foo (const T&, const Args&... args)
 {
   bar<E::size>();
   // Do whatever.
   foo(args...);
 }

template <typename T, typename ... Args, typename E>
std::enable_if_t<!E::value> foo (const T&, const Args&... args)
 {
   hit<E::size>();
   // Do whatever, but different from the previous foo overload.
   foo(args...);
 }

你可能喜欢这个:

template <std::size_t N> void bar() { std::cout << "bar<" << N << ">() called.\n"; }
template <std::size_t N> void hit() { std::cout << "hit<" << N << ">() called.\n"; }

template <typename T>  
struct evaluate : std::bool_constant<std::is_integral_v<T>> 
{   
    static constexpr std::size_t size = sizeof(T);  // Simplified for illustration only.
};  

void foo() { } 

template <typename T, typename... Args>
void foo( const T&, const Args&... args) 
{   
    using X = evaluate<T>;

    if constexpr ( X::value )
    {   
        bar<X::size>();
    }   
    else
    {   
        hit<X::size>();
    }   

    foo( args... );
}   


int main() {
    foo (5, "hello", true);
}

它 "calls" 只有一次 evaluate<T>,这并不重要,但可能更容易阅读。所有模板代码仅在实例化期间使用,这只是个人喜好问题。

正如您提到的 c++17 您可以使用 constexpr if 在您的示例中完全摆脱 SFINAE。这也使得在 foo 的两种变体中重用公共代码行成为可能,这非常好。您可以相信,可执行文件不会有太大差异,但我认为可维护性要好得多!