是将字符串作为模板参数不好

发布于 2025-01-28 05:03:52 字数 1394 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Hello爱情风 2025-02-04 05:03:52

我的问题是编译器是否会推断两个独立的功能,一个用于char [4],另一个用于char [3]?

会。

如果我用上述const char*调用许多弹性,则编译器将产生许多独立的功能?

您不用const char*来调用func。如果仅使用const char*调用func,则功能模板只有一个实例化。

您可以使用const char [4]const char [3]调用该功能,这确实会导致单独的实例。每个唯一的模板参数都会有一个实例化。

这是愚蠢的吗?我应该避免吗?

取决于用例。在许多情况下,优化器只是简单地扩展了所有呼叫内联呼叫,而实例化不会在生成的组装中留下其理论存在的任何痕迹。

My question is if the compiler will deduce two independent functions, one is for char[4] and the other is for char[3]?

It will.

If I call many func with const char* as above, the compiler will generate many independent functions?

You don't call func with const char*. If you did call func with only const char* then there would be only one instantiation of the function template.

You call the function with const char[4] and const char[3] which do cause separate instantiations. There will be an instantiation for each unique sets of template arguments.

Is this stupid? Should I avoid this?

Depends on use case. In many cases, the optimiser simply expands all calls inline and the instantiations won't leave any trace of their theoretical existance in the generated assembly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文