如何在C++中使用可变模板获取可变数量的参数及其大小

huangapple go评论106阅读模式
英文:

How to get variable no of argument with its size in C++ using variadic template

问题

我需要创建一个接受可变数量参数的函数。此函数的目标是获取参数并打印数据。我已成功使用可变模板实现了这个函数,其代码如下:

  1. void print(T var1, Types... var2)
  2. {
  3. cout << "DATA:" << var1 << endl;
  4. print(var2...);
  5. }

但现在我想要打印参数的大小。例如:

  1. char empname[15+1];
  2. print(empname);

它应该打印出empname的大小:16。但它打印出8,因为它打印出了模板参数的数据类型大小。

有没有办法获取参数包中每个参数的大小?因为我仍然在学习C++11的可变模板。

英文:

I need to create a function which take variable no of argument. The objective of this function will be to get argument and print the data. I have succeeded in implementing this function using variadic template whose code is given below:

  1. void print(T var1, Types... var2)
  2. {
  3. cout &lt;&lt; &quot;DATA:&quot;&lt;&lt;var1&lt;&lt; endl ;
  4. print(var2...) ;
  5. }

But now i want to print the size of argument as well . For e.g:

  1. char empname[15+1];
  2. print(empname);

It should print size of empname : 16 . But it print 8 as it is printing size of datatype of template argument.

Any way out to get size of each argument in parameter pack as i am still learning C++11 variadic template.

答案1

得分: 3

我假设你想要做类似以下的事情:

  1. void print() {}
  2. template <typename T, typename... Types>
  3. void print(T var1, Types... var2)
  4. {
  5. cout << "SIZE:" << sizeof(var1) << endl;
  6. print(var2...);
  7. }

当你尝试运行这个代码时:

  1. char empname[15+1];
  2. print(empname);

你得到的结果是 SIZE:8,这是 char * 的大小。当数组传递给函数参数时,它会被转换为指针,这是因为数组指针衰减

所以,如果你手动指定模板参数作为数组引用,那么它就可以正常工作:

  1. char empname[15+1];
  2. print<char (&)[16]>(empname);

然而,这可能不是你想要的。要防止衰减,你可以将参数类型更改为引用。详细说明请参见这里

  1. template <typename T, typename... Types>
  2. void print(T& var1, Types&... var2)
  3. {
  4. cout << "SIZE:" << sizeof(var1) << endl;
  5. print(var2...);
  6. }
英文:

I assume that you are trying to do something like:

(Please put up your entire code you tried, next time)

  1. void print() {}
  2. template &lt;typename T, typename... Types&gt;
  3. void print(T var1, Types... var2)
  4. {
  5. cout &lt;&lt; &quot;SIZE:&quot;&lt;&lt;sizeof(var1)&lt;&lt; endl ;
  6. print(var2...) ;
  7. }

And when you try run this:

  1. char empname[15+1];
  2. print(empname);

You get SIZE:8 which is the size of char *. The array is decayed to a pointer when it is passed to a function parameter. This is because of Array-to-pointer decay.

So if you specify the template parameter manually as an array-reference, then it works fine.

  1. char empname[15+1];
  2. print&lt;char (&amp;)[16]&gt;(empname);

However this probably not be what you want. To prevent the decays, you can just change the argument type to reference. Detailed description is here.

  1. template &lt;typename T, typename... Types&gt;
  2. void print(T&amp; var1, Types&amp;... var2)
  3. {
  4. cout &lt;&lt; &quot;SIZE:&quot;&lt;&lt;sizeof(var1)&lt;&lt; endl ;
  5. print(var2...) ;
  6. }

答案2

得分: 2

只是为了补充@Hanjoung Lee的回答,您也可以像这样获取数组的大小:

  1. template <typename T, std::size_t Size, typename... Types>
  2. void print(T (&)[Size], Types&... var2)
  3. {
  4. std::cout << "SIZE:" << Size << std::endl;
  5. print(var2...);
  6. }
英文:

Just to add another example to @Hanjoung Lee's answer, you could also get the size of your array like this :

  1. template &lt;typename T, std::size_t Size, typename... Types&gt;
  2. void print(T (&amp;)[Size], Types&amp;... var2)
  3. {
  4. std::cout &lt;&lt; &quot;SIZE:&quot; &lt;&lt; Size &lt;&lt; std::endl;
  5. print(var2...);
  6. }

huangapple
  • 本文由 发表于 2020年1月6日 15:45:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定