小编mou*_*yte的帖子

c++ 概念是否会导致编写模板实例以构建输出?

对于具有许多大型和复杂模板实例的库,在我看来,决定是否使用概念的主要考虑因素之一是构建输出的大小是否更小。

使用 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)

c++ templates c++-concepts c++20

4
推荐指数
1
解决办法
69
查看次数

标签 统计

c++ ×1

c++-concepts ×1

c++20 ×1

templates ×1