简体   繁体   English

如何使用 c++ 中的模板调用非成员 function,其中 typename 仅在返回中?

[英]How do you call a non-member function with a template in c++, where the typename is only in the return?

My goal is to have a non member function use a template for a return value.我的目标是让非成员 function 使用模板作为返回值。 This is so I can return a float array, a double array, etc. I get a "couldn't deduce template parameter 'T'" error.这样我就可以返回一个浮点数组、一个双精度数组等。我收到“无法推断模板参数'T'”错误。

Here is the method I am trying to use:这是我尝试使用的方法:

template<typename T>
T* make(double magnitude, double frequency, double radians, double seconds,
        int samplesPerSecond) {
    long samples = length(seconds, samplesPerSecond);
    T *wave = new T[samples];
    for (int i = 0; i < samples; i++) {
        wave[i] = magnitude
                * sin(
                        (2.0 * (M_PI) * frequency * i / samplesPerSecond)
                                + radians);
    }
    return wave;
}

I first tried to call the method normally.我首先尝试正常调用该方法。

double *ds = make(magnitude, frequency, radians, seconds, samplesPerSecond);

I have tried adding the typename after the function name.我尝试在 function 名称之后添加类型名称。

double *ds = make<double>(magnitude, frequency, radians, seconds, samplesPerSecond);

I looked over several pages on the web and they have all covered member functions so far and explained that the type cannot be deduced from a return type, so how do you tell the compiler the type?我查看了 web 上的几页,到目前为止它们已经涵盖了所有成员函数,并解释了不能从返回类型推断出类型,那么你如何告诉编译器类型呢?

You can very much do this with non-member functions, as per the following example, which adds together two values of a templated type:您可以使用非成员函数执行此操作如下例所示,它将模板化类型的两个值相加:

#include <iostream>

template<typename T> T add(const T &val1, const T &val2) {
    return val1 + val2;
}

int main() {
    auto eleven = add<int>(4, 7);
    std::cout << "4   + 7   = " << eleven << '\n';
    auto threePointFour = add<double>(1.1, 2.3);
    std::cout << "1.1 + 2.3 =  " << threePointFour << '\n';
}

The output of that code is, as expected:正如预期的那样,该代码的 output 是:

4   + 7   = 11
1.1 + 2.3 =  3.4

It's possible that your case isn't working because the definition of the templated function may be incorrect - since you haven't supplied that, it's hard to tell for sure.您的案例可能不起作用,因为模板化 function 的定义可能不正确 - 因为您没有提供,所以很难确定。 Hence you may want to see how it compares to my own add function as shown above.因此,您可能想看看它与我自己add的 function 相比如何,如上所示。


As an aside (since your heading indicates this may be what you're attempting), you can also do this without using the templated types in the parameter list (only the return type):顺便说一句(因为您的标题表明这可能是您正在尝试的),您也可以在使用参数列表中的模板类型(仅返回类型)的情况下执行此操作:

#include <iostream>

template<typename T> T addAndHalve(const double &val1, const double &val2) {
    auto x = (val1 + val2) / 2;
    std::cout << "debug " << x << ": ";
    return x;
}

int main() {
    auto eleven = addAndHalve<int>(4, 7);
    std::cout << "(4   + 7  ) / 2 = " << eleven << '\n';
    auto threePointFour = addAndHalve<double>(1.1, 2.3);
    std::cout << "(1.1 + 2.3) / 2 =  " << threePointFour << '\n';
}

You can see this only affects the return value, using double for all parameters:您可以看到这仅影响返回值,对所有参数使用double精度:

debug 5.5: (4   + 7  ) / 2 = 5
debug 1.7: (1.1 + 2.3) / 2 =  1.7

I noticed the c++ standard library had functions like lround(), where the l means the function returns a long, so I ended up making multiple methods too.我注意到 c++ 标准库有类似 lround() 的函数,其中 l 表示 function 返回一个 long,所以我最终也创建了多个方法。

int16_t* makei(double magnitude, double frequency, double radians,
        double seconds, int samplesPerSecond);
float* makef(double magnitude, double frequency, double radians, double seconds,
        int samplesPerSecond);

This violate the "avoid unnecessary overloading" rule I hear echoed so often, and the following code does indeed work.这违反了我经常听到的“避免不必要的重载”规则,下面的代码确实有效。

double *ds = make<double>(magnitude, frequency, radians, seconds, samplesPerSecond);

I had another unknown error that prevented it from working.我有另一个未知错误阻止它工作。

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

相关问题 How do I call a member function inside a non-member function in C++ with an object created outside the non-member function? - How do I call a member function inside a non-member function in C++ with an object created outside the non-member function? 如何在C ++中为模板类包含非成员运算符-重载? - How do in include a non-member operator- overloading for a template class in c++? 嵌套模板类的C ++非成员函数 - C++ non-member functions for nested template classes 从类内部调用非成员函数,但非成员函数将类作为输入(C++) - Call non-member function from inside class, but the nonmember function takes class as input (C++) 模板类的C ++静态非成员函数返回对象 - C++ static non-member function returning object of a template class 如何使用类型名和非类型名模板 arguments 调用通用 function? - How to call a generic function with typename and non-typename template arguments? 我可以在C ++中声明一个非成员函数const吗? - Can I declare a non-member function const in C++? C ++:非成员函数的定义/声明的位置 - C++: location of definition/declaration of non-member function C ++ Postfix /前缀运算符重载为非成员函数 - c++ postfix / prefix operator overload as non-member function 来自非成员函数的C ++虚拟继承 - C++ Virtual Inheritance from a non-member function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM