gpt4 book ai didi

c++ - 具有不同大小的 boost::array 的不同实例是否会生成全新的类?

转载 作者:行者123 更新时间:2023-11-30 01:28:49 25 4
gpt4 key购买 nike

如果我编写这样的代码:

boost::array<10> a1;
boost::array<20> a2;
boost::array<30> a3;

模板会为我生成 3 个不同的类并使我的代码变大吗?

如果是,编译器/链接器是否足够智能以仅包含我实际使用的方法定义?例如:如果我使用 a1 对象的“at”方法,但从未使用 a2 对象的“at”方法,那么 a2 的“at”将被完全丢弃。

最佳答案

Will the template generate 3 different classes for me

是的;具有不同模板参数的模板的每个实例化实际上是不同的类型。

and make the size of my code grow?

极不可能,肯定不会比启用内联更糟糕。

If it does, is the compiler/linker smart enough to only include the methods definitions of what I'm actually using?

是的,除非您将类型显式实例化为一个整体。

For example: if I use the method 'at' of the a1 object, but never user the method 'at' of the a2 object, then the 'at' of the a2 would be totally discarded.

如果您从不使用 at 方法的 a2 ,并且您没有显式实例化 boost::array<T, 20>作为一个整体,那么boost::array<T, 20>::at()从一开始就不会实例化,也没有什么可丢弃的。

关于c++ - 具有不同大小的 boost::array 的不同实例是否会生成全新的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7019856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com