gpt4 book ai didi

C++ 模板无法识别数据类型

转载 作者:行者123 更新时间:2023-11-30 01:34:28 25 4
gpt4 key购买 nike

我有以下简化代码,其中编译器无法识别数据类型,我不确定原因。在代码中,我希望能够传递一个 map ,其中关键字可以是 std::string。 , 一个 char ,或 int .我还希望用户能够选择关联值是否为 float。或 double .此外,根据关联值的类型,我希望返回值是 std::vector<float>std::vector<double> .由于数据类型的可变性,我选择将此问题编码为模板。

// main.cpp
#include "test.hpp"
#include <iostream>
#include <math.h>
#include <map>
#include <tuple>

double func6(std::map<char, double> arr);

int main(int argc, const char * argv[]) {

std::map<char, double> inputs;
inputs['x'] = 2.0;
inputs['y'] = 5.438;

std::tuple<std::vector<double>, std::vector<double>> answer;

ODESolver q;
answer = q.ode_solver(inputs, func6);
// - The line below this was tried and it did not work
// any better than the line of code above this.
// answer = q.ode_solver<char, double>(inputs, func6);
return 0;
}

double func6(std::map<char, double> arr)
{
return arr['y'] * log(arr['y']) / arr['x'];
}

.hpp 文件包含以下信息。

#ifndef test_hpp
#define test_hpp

#include <stdio.h>
#include <tuple>
#include <vector>
#include <map>
#include <functional>


class ODESolver
{
public:
template<class char_type, class real_type>
static inline std::tuple<std::vector<real_type>, std::vector<real_type>>
ode_solver(std::map<char_type, real_type> &inputs,
const std::function<real_type(std::map<char_type, real_type>)>& func)
{
// - This function does not work with the function call
// as written

// - The code in this function is irrelevant, it was just
// created to have returnable information of the correct
// type to test the function call
std::vector<real_type> one = {0.0, 1.0};
std::vector<real_type> two = {0.0, 1.0};
std::tuple<std::vector<real_type>, std::vector<real_type>> three(one, two);
return three;
}
};
#endif /* test_hpp */

编译器不允许使用上面显示的模板,因为它无法识别 answer= q.ode_solver(inputs, func6) 的匹配函数调用。在主程序中。但是,如果我更换 std::function使用以下代码声明它工作正常。

template<class char_type, class real_type>
static inline std::tuple<std::vector<real_type>, std::vector<real_type>>
ode_solver(std::map<char_type, real_type> &inputs,
const std::function<double(std::map<char, double>)>& func)

我所做的只是用我想在这种情况下使用的参数替换模板化参数,但这违背了使用模板的原因。我也试过用 answer = q.ode_solver<char, double>(inputs, func6) 调用函数;它仍然无法识别该功能。我错过了什么?

最佳答案

隐式转换(在这种情况下从函数指针到 std::function)不会在 template argument deduction 中考虑。 .

Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for overload resolution, which happens later.

您可以添加显式转换,例如

answer = q.ode_solver(inputs, static_cast<std::function<double(std::map<char, double>)>>(func6));

或明确指定模板参数(我不确定你为什么说它不起作用,请参阅下面链接的现场演示)

answer = q.ode_solver<char, double>(inputs, func6);

LIVE

或者只是添加另一个模板参数而不是使用 std::function

template<class char_type, class real_type, class F>
static inline std::tuple<std::vector<real_type>, std::vector<real_type>>
ode_solver(std::map<char_type, real_type> &inputs,
const F& func)

关于C++ 模板无法识别数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56284400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com