主体函数体未检测到对重载可变参数模板函数C ++的调用

问题描述 投票:0回答:1

我目前正在学习可变参数模板函数和参数打包/解包。

这是我的代码,

template<typename T, typename U> 
void my_insert(std::vector<int>& v, T& t) {
    int i;
    if (typeid(t).name() == typeid(const char*).name()) {
        i = stoi(t);
    }
    else if (typeid(t).name() == typeid(char).name()) {
        i = t - 48;
    }
    else if (typeid(t).name() == typeid(int).name()) {
        i = t;
    }
    else if (typeid(t).name() == typeid(double).name()) {
        i = static_cast<int>(round(t));
    }
    else if (typeid(t).name() == typeid(bool).name()) {
        if (t) i == 1;
        else i == 0;
    }
    else if (typeid(t).name() == typeid(std::vector<U>).name()) {
        int j = 0;
        while (j < t.size()) {
            my_insert(v, t[j]);
            ++j;
        }
    }
    else return;
    v.push_back(i);
}
template<typename T, typename U, typename ...Args>
void my_insert(std::vector<int>& v, T& t, Args&... args) {
    int i;
    if (typeid(t).name() == typeid(const char*).name()) {
        if (isdigit(t[0])) i = stoi(t);
        // else do nothing
    }
    else if (typeid(t).name() == typeid(char).name()) {
        i = t - 48;
    }
    else if (typeid(t).name() == typeid(int).name()) {
        i = t;
    }
    else if (typeid(t).name() == typeid(double).name()) {
        i = static_cast<int>(round(t));
    }
    else if (typeid(t).name() == typeid(bool).name()) {
        if (t) i == 1;
        else i == 0;
    }
    else if (typeid(t).name() == typeid(std::vector<U>).name()) {
        int j = 0;
        while (j < t.size()) {
            my_insert(v, t[j]);
            ++j;
        }
    }
    //else do nothing
    v.push_back(i);
    my_insert(args...);
}

int main() {

    std::vector<int> v; 

    my_insert(v, "123", "-8", 32, 3.14159, true, true, false, '5', "12.3");

    return 0;
}

[我不知道自己犯了什么错误,因为对我来说,print()函数的完全相同的实现与{ cout << t << endl; print(args...); }一起工作,带有签名<typename T, typename ...Args> void print(const T& t, const Args... args)

我知道可变参数函数可以通过递归调用同一函数的非可变参数重载版本来实现。所谓的基本案例陈述。

话虽这么说,我不确定我做错了什么。

c++ visual-studio variadic-functions packing
1个回答
0
投票
嗯...您的代码中有一些问题。
© www.soinside.com 2019 - 2024. All rights reserved.