Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是C++;是否可以将可变模板包扩展为多种模板类型(如std::shared_ptr)?_C++_Templates_C++17_Variadic Templates_Variadic - Fatal编程技术网

C++ 是C++;是否可以将可变模板包扩展为多种模板类型(如std::shared_ptr)?

C++ 是C++;是否可以将可变模板包扩展为多种模板类型(如std::shared_ptr)?,c++,templates,c++17,variadic-templates,variadic,C++,Templates,C++17,Variadic Templates,Variadic,我有一个签名如下的函数 template <typename FirstType, typename ...NextTypes> static std::vector<std::tuple<std::shared_ptr<FirstType>, std::shared_ptr<NextTypes...>>> 模板 静态std::vector 当我真正的意图是宣布一些意味着 template <typen

我有一个签名如下的函数

    template <typename FirstType, typename ...NextTypes>
    static std::vector<std::tuple<std::shared_ptr<FirstType>, std::shared_ptr<NextTypes...>>>
模板
静态std::vector
当我真正的意图是宣布一些意味着

    template <typename FirstType, typename ...NextTypes>
    static std::vector<std::tuple<std::shared_ptr<FirstType>, std::shared_ptr<NthType>, std::shared_ptr<NthType>>>
模板
静态std::vector
我不希望将模板类型作为共享\u ptr传入。
是否可能/如何?

如果我理解正确,您希望将模板参数包扩展为多个元组元素。以下是如何做到这一点,例如:

template <typename FirstType, typename ...NextTypes>
static std::vector<std::tuple<std::shared_ptr<FirstType>, std::shared_ptr<NextTypes>...>> some_function(FirstType t, NextTypes... n) {
    return { make_tuple(make_shared<FirstType>(t), make_shared<NextTypes>(n)...) };
}

具体来说,当我尝试用三种类型调用此模板函数时,我得到“替换失败…类模板‘shared_ptr’的模板参数太多”
std::shared_ptr
->
std::shared_ptr…
Thank you Miles,事后看来很简单:)包扩展(以及折叠表达式)可能会有点混乱。我记忆将要重复(扩展)的内容的方式是哪个位最接近
。在您的例子中,它是
NthType…
,因此它将尝试执行类似
shared\u ptr
的操作,这是无效的。如果您已经熟悉正则表达式,另一种记住它的方法是它类似于(\d)+。不完全是我正在做的,但仍然解决了它!哈哈。奇怪的是(也许是偶然的)我在这里使用了一些其他的模板函数/recursion/RTTI来从共享基类创建元组向量。
int main() {
    int* ip = nullptr;
    long* lp = nullptr;
    double* dp = nullptr;

    auto v = some_function(ip, lp, dp);
    auto t = v.front();

    auto sp = get<1>(t);
    if (sp) cout << "Not NULL" << endl;

    return 0;
}