C++,模板与自动
我正在为库工作,我必须推广一些功能,也就是说,这些功能需要处理传递的不同类型的数据作为参数。
例如:
Class myClass
{
public:
int num;
char mark;
vector <bool> boolVector;
}
myClass cl;
void myFunction(any/auto/T value, int operation)
{
switch(operation)
{
case A:
{
cl.num = value; break;
}
case B:
{
cl.mark = value; break;
}
case C:
{
for(int i = 0; i < 10; i++)
{
cl.vector.at(i) = value[i];
}
break;
}
}
}
我对两者之间的使用不确定:
- 有些
- 模板
- :
显然是一个非常微不足道的示例,而我的代码过程中的实际功能更为复杂
谁
- 是最快的?
- 谁使用最多的记忆?
- 谁是最常用的?
- 您喜欢哪个,为什么?
I'm working for my library, and i must generalize some functions, that is, these functions need to handle different types of data passed as parameters.
ex:
Class myClass
{
public:
int num;
char mark;
vector <bool> boolVector;
}
myClass cl;
void myFunction(any/auto/T value, int operation)
{
switch(operation)
{
case A:
{
cl.num = value; break;
}
case B:
{
cl.mark = value; break;
}
case C:
{
for(int i = 0; i < 10; i++)
{
cl.vector.at(i) = value[i];
}
break;
}
}
}
I'm undecided on what to use between:
- any,
- some
- template
obviously, this is a very trivial example, while the real functions in my code process this data more complicatedly
So:
- who is the fastest?
- Who uses the most memory?
- Who is the most used?
- Which do you prefer and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tl; dr您可能应该重新考虑您的设计,
auto
,模板参数或std ::任何
真正适用于您的功能。在这种情况下,
auto
和模板类型参数是同一件事。这
只是速记的,
因为它们都是模板,因此在编译时可以解决。那就是
foo(some_int)
和foo(some_char)
最终调用不同的 functionsfoo&lt; int&gt;
and and <编译器从您编写的模板中生成的代码> foo&lt; char&gt; 。因此,没有运行时性能开销(除了增加可执行文件尺寸外)。这也意味着编译器可以进行类型检查,以确保您不会尝试使用无法做的对象做某事。这意味着给出任何类型的参数必须在函数的所有分支中有效;即使在运行时永远不会为给定类型的参数带它们。std ::任何
是完全不同的。std ::任何
是一个基于类型的包装类类,可以容纳任何类型的对象。此外,给定的std ::任何
对象可以在其一生中从持有一种类型变为另一种类型。这意味着,如果您使用std ::任何
,则编译器无法为您进行任何类型检查。类型检查只能在运行时完成,这意味着std ::任何
使用相当麻烦,并且具有一定的运行时性能开销。它有其用途,但相当少,相当远。如果可以的话,您可能应该将功能分为多个。例如:(
请注意,要警惕
std :: vector&lt; bool&gt;
,它不是一个真正的容器,在某些情况下表现出奇怪的行为)。TL;DR You should probably re-think your design
None of
auto
, a template parameter, orstd::any
really work for your function as written.In this context,
auto
and a template type parameter are the same thing.That is
is just shorthand for
Since they're both templates, they are resolved at compile time. That is
foo(some_int)
andfoo(some_char)
end up calling different functionsfoo<int>
andfoo<char>
that the compiler generates from the template that you wrote. As such, there's no runtime performance overhead (aside from possibly increased executable size). It also means the compiler can do type-checking to ensure you're not trying to do something with an object that it can't do. That means that whatever type is given as the parameter must be valid in all branches of the function; even if they'll never be taken for a given type of parameter at runtime.std::any
is something entirely different.std::any
is a type-erased wrapper class that can hold objects of any type. Additionally, a givenstd::any
object can change from holding one type to another over the course of its lifetime. That means that the compiler cannot do any type checking for you if you usestd::any
. Type checking can only be done at runtime, which meansstd::any
is fairly cumbersome to use and has some runtime performance overhead. It has its uses, but they're fairly few and far between.If you can, you should probably split your function into multiple. For instance:
(Note, be wary of
std::vector<bool>
, it isn't a real container and behaves strangely in some situations).