C ++具有不同返回类型的相同函数参数 [英] C++ same function parameters with different return type

查看:154
本文介绍了C ++具有不同返回类型的相同函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一些方法来模拟C ++中的函数返回类型的重载。

I need to find some way to mock an overload of a function return type in C++.

我知道没有办法直接做,但我希望有一些开箱即用的方法。
我们正在为用户创建一个API,他们将传递一个数据字符串,该字符串根据字符串信息检索值。这些值是不同的类型。实质上,我们希望让他们这样做:

I know that there isn't a way to do that directly, but I'm hoping there's some out-of-the-box way around it. We're creating an API for users to work under, and they'll be passing in a data string that retrieves a value based on the string information. Those values are different types. In essence, we would like to let them do:

int = RetrieveValue(dataString1);
double = RetrieveValue(dataString2);
// Obviously, since they don't know the type, they wouldn't use int =.... It would be:
AnotherFunction(RetrieveValue(dataString1)); // param of type int
AnotherFunction(RetrieveValue(dataString2)); // param of type double

但是这在C ++中不起作用。
现在,我们正在设置它们,以便他们调用:

But that doesn't work in C++ (obviously). Right now, we're having it set up so that they call:

int = RetrieveValueInt(dataString1);
double = RetrieveValueDouble(dataString2);

但是,我们不希望他们需要知道它们的数据字符串的类型。

However, we don't want them to need to know what the type of their data string is.

不幸的是,我们不允许使用外部库,因此没有使用Boost。

Unfortunately, we're not allowed to use external libraries, so no using Boost.

我们可以解决这个问题吗?

Are there any ways we can get around this?

为了澄清,我明白C ++不能原生地做。但是必须有一些方法来绕过它。例如,我想到了做RetrieveValue(dataString1,GetType(dataString1))。这并不真正解决什么,因为GetType也只能有一个返回类型。但我需要类似的东西。

Just to clarify, I understand that C++ can't natively do it. But there must be some way to get around it. For example, I thought about doing RetrieveValue(dataString1, GetType(dataString1)). That doesn't really fix anything, because GetType also can only have one return type. But I need something like that.

我知道这个问题以前被问过,但在不同的意义。我不能使用任何明显的答案。我需要一个完全开箱即用的东西,对我有用,而在其他问题的任何答案都不是这样。

I understand that this question has been asked before, but in a different sense. I can't use any of the obvious answers. I need something completely out-of-the-box for it to be useful to me, which was not the case with any of the answers in the other question asked.

推荐答案

您必须从这开始:

template<typename T>
T RetrieveValue(std::string key)
{
     //get value and convert into T and return it
}

为了支持这个函数,你必须多工作一点,为了将值转换为 T 。一种简单的转换价值的方法可以是:

To support this function, you've to work a bit more, in order to convert the value into the type T. One easy way to convert value could be this:

template<typename T>
T RetrieveValue(std::string key)
{
     //get value
      std::string value = get_value(key, etc);

      std::stringstream ss(value);
      T convertedValue;
      if ( ss >> convertedValue ) return convertedValue;
      else throw std::runtime_error("conversion failed");
}

请注意,您仍然必须将此函数调用为:

Note that you still have to call this function as:

int x = RetrieveValue<int>(key);

您可以避免提及 int 您可以改为:

You could avoid mentioning int twice, if you could do this instead:

Value RetrieveValue(std::string key)
{
     //get value
      std::string value = get_value(key, etc);
      return { value };
}

其中 Value 实现为:

struct Value
{
    std::string _value;

    template<typename T>
    operator T() const   //implicitly convert into T
    {
       std::stringstream ss(_value);
       T convertedValue;
       if ( ss >> convertedValue ) return convertedValue;
       else throw std::runtime_error("conversion failed");
    }
}

然后你可以这样写:

int    x = RetrieveValue(key1);
double y = RetrieveValue(key2);

这是您想要的,对吗?

这篇关于C ++具有不同返回类型的相同函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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