C ++ - 将模板类型对象转换为特定数据类型 [英] C++ - Cast template-type object to specific data types

查看:190
本文介绍了C ++ - 将模板类型对象转换为特定数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个模板函数 searchByCriteria< T> 其中我想能够使用 string double 。我有一个自定义对象的列表,它有 string double 属性,我想能够使用此函数检查条件值(由用户输入的任何类型),以检查相同类型的对象属性中的匹配。

I'm using a template function searchByCriteria<T> where I would like to be able to run the function using both a string and a double. I have a list of custom-defined objects that have string and double attributes, and I would like to be able to use this function to check the criteria value (of whatever type, entered by the user), to check for matches in the object attributes of the same type.

用户输入一个double值,检查对象集合是否匹配double值。用户输入一个字符串值,检查对象集合中匹配的字符串值。

I.e. User enters a double value, check the object collection for matching double values. User enters a string value, check the object collection for matching string values.

我遇到的问题是一旦输入值,它被传递给另一个模板函数对照列表中的元素进行检查。此时,作为参数传递的 T 对象需要转换为双精度或字符串以允许检查匹配。

The problem I am having is once the value is entered, it is passed to another template function to be checked against the elements in the list. And at this point, the T object that is passed as a parameter, needs to be converted to either a double or a string to allow checking for matches.

这是此部分的代码:

//get a sub-list of transactions
//of all that match specified search criteria
template <typename T>
const TransactionList TransactionList::getTransactionsForSearchCriteria(T criteria) const {
    //make a copy of list to avoid deleting existing data
    TransactionList copy(*this);
    //to have appropriate transactions added in
    //then returned as copy
    TransactionList ret;

    //////////////////////////////////////////
    //before checking for matches can start///
    //must ID datatype of T instance//////////
    //////////////////////////////////////////

    //check all transactions until list empty
    while (copy.size() > 0)
    {
        //check that criteria matches transaction attribute
        if (/*converted criteria matches corresponding attribute*/)
        {
            //flag as match
        }
    }
}

可以看到,在输入while循环以检查匹配之前,需要将参数值 criteria 转换回特定的数据类型。我对如何做到这一点有点失落,因为我不知道任何铸造方法在C + +,这将是有用的在这种情况下。

As you can see, the parameter value criteria needs to be converted back into a specific data type before the while loop can be entered to check for matches. I am at a slight loss as to how to do this, as I am not aware of any casting methods in C++ that would be useful in this situation.

我可以想到会是这样:

try
{
    //example method
    convertToDouble(criteria);
}
catch (SomeKindOfNumberException ex)
{
    //cannot be a double
    //so must be string
    convertToString(criteria);
}

非常感谢任何帮助。

感谢,
Mark

Thanks, Mark

推荐答案

这样的事情怎么样?

#include <iostream>

template<typename T>
T FuncB(T x) {
    std::cout << "generic T ";
    return x;
}

template<>
double FuncB<double>(double x) {
    std::cout << "double ";
    return 0.123;
}

template<>
std::string FuncB<std::string>(std::string x) {
    std::cout << "string ";
    return "xyz";
}

template <typename T>
void FuncA(T param) {
    std::cout << "FuncA: ";
    T tmp = FuncB(param);
    std::cout << tmp << std::endl;
}

int main() {
    std::string s = "abc";

    FuncA(0.1);
    FuncA(s);
    FuncA(1);
}

FuncA可以接收任何T,但它使用FuncB, 。如果你想要你可以保留或删除FuncB(T)支持/避免使用未知类型。

FuncA can receive any T but it uses FuncB which is specialized for some specific types. If you want you can keep or delete FuncB(T) to support/avoid using unknown types.

上一个代码的输出如下:

The output of the previous code looks like:

FuncA: double 0.123
FuncA: string xyz
FuncA: generic T 1

这篇关于C ++ - 将模板类型对象转换为特定数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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