对于具有许多大型和复杂模板实例的库,在我看来,决定是否使用概念的主要考虑因素之一是构建输出的大小是否更小。
使用 SFINAE,我的理解是以下代码将导致模板实例化std::is_function<bar>并std::enable_if<true, bool>包含在构建输出中,增加其大小(尽管对于此示例来说是微不足道的):
#include <type_traits>
template<typename F,
typename = std::enable_if_t<
std::is_function<F>::value,
bool> = true>
void foo(F& f)
{
// do some stuff with f
}
void g();
int main()
{
foo(g);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果使用基于的 C++20 概念std::is_function,显然模板必须被实例化以检查它。但是那个实例化然后写入最终的构建输出吗?这是否因编译器实现而异?
#include <type_traits>
template<typename F>
concept Function = std::is_function<F>::value;
template<Function F>
void foo(F& f)
{
// do some stuff with f
}
//...
Run Code Online (Sandbox Code Playgroud)