简体   繁体   English

在C ++中使用具有不同可能类型的参数重用函数调用

[英]Reuse function call with argument of different possible types in c++

I am wondering if there is a way to reuse the call to the function template_fun in the following c++ code. 我想知道是否有一种方法可以在以下c ++代码中重用对函数template_fun的调用。

#include <iostream>
#include <cstdlib>
#include <ctime>


template <typename T>
double template_fun(T arg)
{
    double a = 1.1;
    a += (double)arg;
    return a;
}

int main()
{
    std::srand(std::time(0));
    int r = std::rand() % 2;
    double out;

    switch(r)
    {
        case 0:
        {
            int arg = 1;
            out = template_fun(arg);
            break;
        }
        case 1:
        {
            double arg = 1.2;
            out = template_fun(arg);
            break;
        }
    }

    std::cout << out << "\n";
}

Since the line out = template_fun(arg); 由于该行out = template_fun(arg); is repeated, I was hoping there was a way to reuse it somehow. 重复一遍,我希望有一种以某种方式重用它的方法。 Obviously, this issue of calling a template function with different input data types depending on an input is really what I'm getting at. 显然,这个问题取决于我根据输入调用具有不同输入数据类型的模板函数的问题。 The code I'm working on is much more complex. 我正在处理的代码要复杂得多。 I am not particularly hopeful for a clever solution, since it would probably mean defining the data type of arg at runtime. 对于一个聪明的解决方案,我并不特别希望,因为这可能意味着在运行时定义arg的数据类型。 But perhaps I am missing something. 但是也许我错过了一些东西。

Thank you in advance for your help! 预先感谢您的帮助! Much appreciated. 非常感激。

You may use some variant class, and do something like: 您可以使用一些variant类,并执行以下操作:

struct template_fun : boost::static_visitor<double>
{
    template <typename T>
    double operator() (T arg) const
    {
        double a = 1.1;
        a += (double)arg;
        return a;
    }
};

int main()
{
    std::srand(std::time(0));
    int r = std::rand() % 2;
    boost::variant<int, double> arg;
    double out = 0.0;

    switch(r)
    {
        case 0: { arg = 1;   break; }
        case 1: { arg = 1.2; break; }
    }
    out = boost::apply_visitor(template_fun{}, arg);
    std::cout << out << "\n";
}

Demo 演示

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

相关问题 C ++调用函数有许多不同的类型 - C++ call function with many different types 根据c++模板function的参数类型解析不同类型 - Resolving to different types based on the argument types of a c++ template function 在C ++中,是否可以将两种不同的数据类型传递给模板函数? - In C++, is it possible to pass in two different data types to a template function? 如何以编程方式调用具有不同数据类型的C ++模板化函数? - How to call a C++ templated function with different data types programmatically? 如何重用基于 C++ 中不同类型的表? - How to reuse table that would be based on different types in C++? 是否可以在c ++中声明具有不同类型的变量? - Is it possible declare a variable with different types in c++? 在c ++中是否可以指向不同类型的指针? - Are array of pointers to different types possible in c++? C ++ - 是否可以从模板中的成员函数类型中提取类和参数类型? - C++ - is it possible to extract class and argument types from a member function type in a template? 是否可以在 C++ 中将类作为函数的参数传递 - Is it possible to pass a class as an argument of a function in C++ 模板化一个外部“ C”函数,以使用不同类型从C ++调用Fortran函数 - Template an extern “C” function to call Fortran functions from C++ with different types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM