如何实现模板方法的自动类型转换? [英] How to realize automatic type conversion for template methods?

查看:31
本文介绍了如何实现模板方法的自动类型转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自己编写的模板方法从/向 Qt 的 QSettings 加载和保存设置.QSettings 接收/返回 QVariantQVariant 类型的数据的构造函数采用基本类型,如 intdouble,但不是 std::string.

因此,我的模板方法可以很容易地用于 intdouble,但是在使用 std::string 时,我收到编译错误消息没有匹配的函数调用,因为没有从 std::__cxx11::basic_stringQVariant 的构造函数接受的类型的已知转换.>

当然我可以通过编写我的模板方法的特化来解决问题.尽管如此,我还是犹豫要不要这样做,因为正是 一个 小调用导致了这个问题.其余代码适用于所有类型.

是否可以提供用户定义的类型转换,以便编译器在编译模板方法时可以自动使用类型转换?

这是一个最小的例子,它试图在没有 Qt 类的情况下重现这种情况:

#include #include <向量>A级{民众:A(整数){}A(双){}};std::vector全球存储;模板 无效存储(类型元素){//大量冗长的存储准备代码//...////最终存储globalStorage.push_back(A(element));}//TODO:模板方法的特化解决了这个问题.在那儿//另一种提供用户定义类型转换的方法?模板 <>void store(std::string 元素){//大量冗长的存储准备代码//...//globalStorage.push_back(A(std::stoi(element)));}int main(){双数1 = 1.0;int number2 = 2.0;浮点数 3 = 3.0;商店(编号 1);商店(编号 2);商店(编号 3);std::string number4 = "4";商店(编号 4);返回0;}

为转发到您的方法的 std::string 创建一个重载的非模板.

template 无效存储(类型元素){//大量冗长的存储准备代码//...////最终存储globalStorage.push_back(A(element));}无效存储(const std::string& s){//根据需要将 s 转换为 QString 或 char* 或数字自动converted_element = ...;存储(转换元素);}

I am using self-written template methods to load and save settings from/into Qt's QSettings. QSettings receive/return data of type QVariant and QVariant has constructors taking basic types like int and double, but not std::string.

My template method can therefore easily be used for int and double, but when using std::string I get compile error messages that there is no matching function call, because there is no known conversion from std::__cxx11::basic_string<char> to the types accepted by QVariant's constructors.

Of course I can solve the problem by writing a specialization of my template method. Still, I am hesitating to do that, because there is exactly one little call that causes the problem. The rest of the code is just fine for all types.

Is it possible to provide a user-defined type conversion such that the compiler can automatically use for type-conversion when compiling the template method?

Here is a minimal example that tries to reproduce the situation without Qt classes:

#include <string>
#include <vector>

class A
{
public:

    A(int)
    {
    }

    A(double)
    {
    }
};

std::vector<A> globalStorage;

template <typename Type>
void store(Type element)
{
    // A lot of lengthy storage preparation code
    // ...
    //

    // Final Storage
    globalStorage.push_back(A(element));
}

// TODO: A specialization of the template method solves the problem. Is there
// another way to provide a user-defined type conversion?
template <>
void store<std::string>(std::string element)
{
    // A lot of lengthy storage preparation code
    // ...
    //

    globalStorage.push_back(A(std::stoi(element)));
}

int main()
{
    double number1 = 1.0;
    int number2 = 2.0;
    float number3 = 3.0;

    store (number1);
    store (number2);
    store (number3);

    std::string number4 = "4";
    store(number4);

    return 0;
}

解决方案

Create an overloaded non-template for std::string that forwards to your method.

template <typename Type>
void store(Type element)
{
    // A lot of lengthy storage preparation code
    // ...
    //

    // Final Storage
    globalStorage.push_back(A(element));
}

void store(const std::string& s) {
    //convert s to QString or char* or a number, as you want
    auto converted_element = ...;
    store(converted_element);
}

这篇关于如何实现模板方法的自动类型转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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