简体   繁体   English

C++,模板与自动与任何

[英]C++, Template vs Auto vs Any

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?谁用的memory最多?
  3. Who is the most used?谁用得最多?
  4. Which do you prefer and why?你更偏向于哪个,为什么?

TL;DR You should probably re-think your design TL;DR 你可能应该重新考虑你的设计

None of auto , a template parameter, or std::any really work for your function as written. auto 、模板参数或std::any真正适用于您所写的 function 。

In this context, auto and a template type parameter are the same thing.在这种情况下, auto和模板类型参数是一回事。
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.那就是foo(some_int)foo(some_char)最终调用编译器从您编写的模板生成的不同函数foo<int>foo<char> 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.这也意味着编译器可以进行类型检查,以确保您不会尝试用 object 做一些它不能做的事情。 That means that whatever type is given as the parameter must be valid in all branches of the function;这意味着作为参数给出的任何类型都必须在 function 的所有分支中都有效; even if they'll never be taken for a given type of parameter at runtime.即使它们在运行时永远不会被用于给定类型的参数。


std::any is something entirely different. std::any是完全不同的东西。 std::any is a type-erased wrapper class that can hold objects of any type. std::any是一个类型擦除的包装器 class ,它可以保存任何类型的对象。 Additionally, a given std::any object can change from holding one type to another over the course of its lifetime.此外,给定的std::any object 可以在其生命周期内从持有一种类型更改为另一种类型。 That means that the compiler cannot do any type checking for you if you use std::any .这意味着如果您使用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.类型检查只能在运行时完成,这意味着std::any使用起来相当麻烦,并且有一些运行时性能开销。 It has its uses, but they're fairly few and far between.它有它的用途,但它们很少而且相差甚远。


If you can, you should probably split your function into multiple.如果可以,您可能应该将 function 拆分为多个。 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). (注意,要小心std::vector<bool> ,它不是真正的容器,在某些情况下会表现得很奇怪)。

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

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