将具有不同类型的模板参数包提取到double的向量中会产生警告 [英] extracting a template parameter pack with different types into a vector of doubles produces warnings

查看:44
本文介绍了将具有不同类型的模板参数包提取到double的向量中会产生警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将基本相同但采用不同数量参数的类的负载转换为单个模板类.因此,我创建了一个模板类示例(不是真正的代码,仅作为示例):

I am trying to convert a load of classes that are basically the same but take different number of parameters into a single template class. So I have create a template class example (not real code - just for example):

// The template type
template<typename... Args>
class tester_template
{
public:
    void process(Args... args)
    {
        // Create a vector to put the args into. use double since that can hold all of the types
        // that I am using (well enough for me anyway). But I get a lot of "narrowing" warnings
        // this is understandable, but I want to have no warnings (some sort of cast on a 
        // parameter pack??)
        std::vector<double> args_vect = {args...};
        for (auto arg : args_vect)
        {
            std::cout << arg << " ";
        }
        std::cout << std::endl;
    };
};

我这样运行:

// Same with one template class
tester_template<double> template1;
tester_template<int, int> template2;
tester_template<int, int, int> template3;
tester_template<float> template4;
tester_template<float, double, int> template5;

template1.process(1.123);           // ok
template2.process(2, 2);            // Warnings
template3.process(3, 2, 3);         // Warnings
template4.process(4.4f);            // Warnings 
template5.process(5.5f, 2.234, 3);  // Warnings

此处带有警告的完整示例以及模板类替换的先前的许多类的示例: https://rextester.com/RBEA68379

Full example here with warnings and with example of previous many classes that the template class replaces: https://rextester.com/RBEA68379

因此,我了解错误/警告消息(基本上,如果进行转换,我可能会丢失数据).但是我想禁止警告-也许通过强制转换.但是我不知道如何使用参数包来执行此操作-也许我错过了它,但是我没有在线找到它.

So I understand the error/warning message (basically I might lose data if I convert). But I want to inhibit the warnings - perhaps by casting. But I have not a clue how to do this with a parameter pack - maybe I am missing it, but I did not find it online.

我猜两个问题:

  1. 我可以抛弃(或其他不会关闭警告的方法)吗?
  2. 我只是试图将参数包提取到可以迭代的结构中,或者我在做什么,还是有更好的方法呢?

推荐答案

是的,您可以轻松地转换参数:

Yes, you can cast the arguments easily:

std::vector<double> args_vect = {static_cast<double>(args)...};

并且没有发出警告.

这是一个演示.

正如@NathanOliver的评论中指出的那样,如果您只想打印所有可变参数,则可以执行以下操作:

As pointed out in the comments by @NathanOliver, if you just want to print all the variadic arguments, you could do:

void process(Args... args)
{
   ((std::cout << args << " "), ...);
   std::cout << std::endl;
};

那么您不必担心任何类型的转换.

Then you don't have to worry about conversions of any kind.

这是演示.

您还可以使用 sizeof ... 来计算传入的参数数量,并使用该信息将其分派给具有固定数量参数的函数:

You can also use sizeof... to figure out the number of arguments passed in, and use that information to dispatch to functions that take a fixed number of parameters:

void process1arg(double ) {}
void process2arg(double , double ) {}
void process3arg(double , double  , double ) {}

void process(Args... args)
{  
    ((std::cout << args << " "), ...);
    std::cout << std::endl;

    if constexpr (sizeof...(args) == 1)
       process1arg(args...);
    if constexpr (sizeof...(args) == 2)
       process2arg(args...);
    if constexpr (sizeof...(args) == 3)
       process3arg(args...);
};

请注意,您需要 if constexpr 而不是常规的 if ,因为否则,当参数数量不匹配时,代码将无法编译.

Note that you need if constexpr and not regular if, since otherwise the code will not compile when the number of arguments don't match.

这是一个演示.

这篇关于将具有不同类型的模板参数包提取到double的向量中会产生警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆