Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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++ 将参数包解包到std::initializer\u列表中?_C++_C++11_Templates_Variadic Templates - Fatal编程技术网

C++ 将参数包解包到std::initializer\u列表中?

C++ 将参数包解包到std::initializer\u列表中?,c++,c++11,templates,variadic-templates,C++,C++11,Templates,Variadic Templates,是否有任何方法可以使用参数包创建对象的初始值设定项列表或将对象插入到std::vector?我遇到的问题是,我看到的所有参数包示例都使用传递的参数使编译器能够区分需要调用的函数。问题是,我没有要传递给函数的参数,只有类型 例如: // compiler cannot determine which function is correct namespace impl { template<class T, class... U> void get_type_impl(

是否有任何方法可以使用参数包创建对象的初始值设定项列表或将对象插入到
std::vector
?我遇到的问题是,我看到的所有参数包示例都使用传递的参数使编译器能够区分需要调用的函数。问题是,我没有要传递给函数的参数,只有类型

例如:

// compiler cannot determine which function is correct
namespace impl
{
    template<class T, class... U>
    void get_type_impl(std::vector<MetaClass*>& types)
    {
        // rusty::get<T>(); just retrieves a MetaClass object for the specified type
        types.emplace_back(rusty::get_type<T>());
        impl::get_type_impl<U...>(types);
    }

    template <class T>
    void get_type_impl(std::vector<MetaClass*>& types)
    {
        types.emplace_back(rusty::get_type<T>());
    }
}

template <class... T>
std::vector<MetaClass*> get_types()
{
    std::vector<MetaClass*> types;
    types.reserve(sizeof...(T));
    impl::get_type_impl<T...>(types);
    return types;
}

解决方案是强制其中一个
get\u type\u impl
采用至少2种模板类型,另一个仅采用1种。这会在签名中产生足够的差异,以便编译器确定哪个函数是正确的。

不确定是否理解,但。。。在我看来,您正在寻找以下内容(注意:未测试代码):

并将其替换为以下接地情况

template <int = 0>
void get_type_impl (std::vector<MetaClass*> const &)
 { }

,被劫持(感谢默认的not type模板参数
int=0
)到地面情况。

我不清楚您真正想要实现什么。xy问题?我看到一个类型列表,但没有参数包,因为您没有参数。那么你真正想要什么呢?另一个问题;您的示例代码有什么问题?它没有你想要的功能吗?@Klaus我的道歉我想我误解了参数包是什么。我更新为“问题”,希望强调我正在努力实现的目标。@Yakk AdamNevraumont我遇到的问题不是缺少功能,而是功能的错误实现/问题的更好解决方案。错误的实现是,两个函数签名可能会完全相同,从而导致编译器对调用哪个函数感到困惑。您最初的想法是正确的。您建议的另一个实现与我现在的实现非常接近。我更新了我的问题,突出了我所做的修复。我认为问题在于我误解了参数包是什么,这导致了很大一部分混乱。@Matthew第一个实现有什么问题?“它更简单、更清晰。@MichaelVeksler上面的第一个实现在我尝试它时不起作用。这是我在实现现在的实现之前的第一次尝试。@Matthew这很奇怪,因为我设法测试并运行了一个类似的解决方案,用于
template const std::type_info*get_type(){return&typeid(T);}
这在C++11支持下工作(不需要C++14):
template std::vector get_types(){return{get_type()…};}
,这与答案中的代码几乎相同。您发现了什么错误?@MichaelVeksler让我再次测试一下。我确实有一个稍微不同的语法,并且知道我现在使用的方法可以工作,所以我可能会搞错一些东西,并迅速切换到验证我在建议的实现中没有做错什么。
namespace impl
{
    template <class T>
    void get_type_impl(std::vector<MetaClass*>& types)
    {
        types.emplace_back(rusty::get_type<T>());
    }
    template<class T0, class T1, class... Tn>
    void get_type_impl(std::vector<MetaClass*>& types)
    {
        types.emplace_back(rusty::get_type<T0>());
        impl::get_type_impl<T1, Tn...>(types);
    }
}

template <class... T>
std::vector<MetaClass*> get_types()
{
    std::vector<MetaClass*> types;
    types.reserve(sizeof...(T));
    impl::get_type_impl<T...>(types);
    return types;
}
template <typename ... Ts>
std::vector<MetaClass*> get_types()
 { return { rusty::get_type<Ts>()... }; }
template <class T>
void get_type_impl(std::vector<MetaClass*>& types)
{
    types.emplace_back(rusty::get_type<T>());
}
template <int = 0>
void get_type_impl (std::vector<MetaClass*> const &)
 { }
impl::get_type_impl<>(types);