C++,模板与自动

发布于 2025-02-12 03:24:26 字数 856 浏览 2 评论 0原文

我正在为库工作,我必须推广一些功能,也就是说,这些功能需要处理传递的不同类型的数据作为参数。

例如:

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

我对两者之间的使用不确定:

  • 有些
  • 模板

显然是一个非常微不足道的示例,而我的代码过程中的实际功能更为复杂

  1. 是最快的?
  2. 谁使用最多的记忆?
  3. 谁是最常用的?
  4. 您喜欢哪个,为什么?

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:

  1. who is the fastest?
  2. Who uses the most memory?
  3. Who is the most used?
  4. Which do you prefer and why?

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

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

发布评论

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

评论(1

凡间太子 2025-02-19 03:24:26

tl; dr您可能应该重新考虑您的设计,

auto,模板参数或std ::任何真正适用于您的功能。

在这种情况下,auto和模板类型参数是同一件事。

void foo(auto value)
{
    // ...
}

只是速记的,

template <typename T>
void foo(T value)
{
    // ...
}

因为它们都是模板,因此在编译时可以解决。那就是foo(some_int)foo(some_char)最终调用不同的 functions foo&lt; int&gt; and and <编译器从您编写的模板中生成的代码> foo&lt; char&gt; 。因此,没有运行时性能开销(除了增加可执行文件尺寸外)。这也意味着编译器可以进行类型检查,以确保您不会尝试使用无法做的对象做某事。这意味着给出任何类型的参数必须在函数的所有分支中有效;即使在运行时永远不会为给定类型的参数带它们。


std ::任何是完全不同的。 std ::任何是一个基于类型的包装类类,可以容纳任何类型的对象。此外,给定的std ::任何对象可以在其一生中从持有一种类型变为另一种类型。这意味着,如果您使用std ::任何,则编译器无法为您进行任何类型检查。类型检查只能在运行时完成,这意味着std ::任何使用相当麻烦,并且具有一定的运行时性能开销。它有其用途,但相当少,相当远。


如果可以的话,您可能应该将功能分为多个。例如:(

void myFunctionA(int num)
{
    cl.num = num;
}

void myFunctionB(char mark)
{
    cl.mark = mark;
}

void myFunctionC(std::vector<bool> boolVector)
{
    cl.boolVector = boolVector;
}

请注意,要警惕std :: vector&lt; bool&gt;,它不是一个真正的容器,在某些情况下表现出奇怪的行为)。

TL;DR You should probably re-think your design

None of auto, a template parameter, or std::any really work for your function as written.

In this context, auto and a template type parameter are the same thing.
That is

void foo(auto value)
{
    // ...
}

is just shorthand for

template <typename T>
void foo(T value)
{
    // ...
}

Since they're both templates, they are resolved at compile time. That is foo(some_int) and foo(some_char) end up calling different functions foo<int> and foo<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 given std::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 use std::any. Type checking can only be done at runtime, which means std::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:

void myFunctionA(int num)
{
    cl.num = num;
}

void myFunctionB(char mark)
{
    cl.mark = mark;
}

void myFunctionC(std::vector<bool> boolVector)
{
    cl.boolVector = boolVector;
}

(Note, be wary of std::vector<bool>, it isn't a real container and behaves strangely in some situations).

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