简体   繁体   English

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

[英]Do c++ concepts cause template instantiations to be written to build output?

For libraries with many large and complex template instantiations, it seems to me that one of the major considerations in deciding whether to use concepts would be whether or not the build output is smaller in size.对于具有许多大型和复杂模板实例化的库,在我看来,决定是否使用概念的主要考虑因素之一是构建 output 的大小是否更小。

With SFINAE, my understanding is that the following code will cause the template instantiations std::is_function<bar> and std::enable_if<true, bool> to be included in the build output, increasing its size (albeit marginally for this example):对于 SFINAE,我的理解是以下代码将导致模板实例化std::is_function<bar>std::enable_if<true, bool>包含在构建 output 中,从而增加其大小(尽管在本示例中略有增加) :

#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;
}

If a C++20 concept based on std::is_function is used instead, obviously the template will have to be instantiated to check it.如果改为使用基于std::is_function的 C++20 概念,显然必须实例化模板才能对其进行检查。 But is that instantiation then written to the final build output?但是该实例化是否随后写入最终版本 output? And does this differ depending on compiler implementation?这是否因编译器实现而异?

#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
}
//...

It seems you are worried about code size.您似乎担心代码大小。

Classes, and here I include instantiations of class templates, are just abstract pieces of information that the compiler knows about.类,在这里我包括 class 模板的实例化,只是编译器知道的抽象信息片段。 They do not contribute to the code size.它们不会影响代码大小。

Only functions, including member functions, contribute to code size.只有函数(包括成员函数)会影响代码大小。 Member functions of class templates contribute to code size only when they are instantiated because the way in which they are used requires it (the C++ standard calls it " ODR-used "). class 模板的成员函数仅在它们被实例化时才会影响代码大小,因为它们的使用方式需要它(C++ 标准将其称为“ ODR-used ”)。

In your examples, no member functions of std::is_function and std::enable_if are ODR-used, so they are not instantiated, and they do not contribute to code size.在您的示例中,没有std::is_functionstd::enable_if的成员函数是 ODR 使用的,因此它们没有被实例化,并且它们不影响代码大小。

But is that instantiation then written to the final build output?但是该实例化是否随后写入最终版本 output?

No code is generated.不生成代码。 But typically compilers write debugging information to the output.但通常编译器会将调试信息写入 output。 In that sense, something is written to the output.从这个意义上说,有些东西写入 output。

And does this differ depending on compiler implementation?这是否因编译器实现而异?

As far as debugging information is concerned: yes.就调试信息而言:是的。 But whether an instantiation occurs is governed by the rules of the language, and there should not be a difference between compilers.但是是否发生实例化是由语言的规则决定的,编译器之间应该没有区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM