为使用数组,向量,结构等传递给可变函数或可变参数模板函数w / out的所有参数指定一个类型? [英] Specifying one type for all arguments passed to variadic function or variadic template function w/out using array, vector, structs, etc?

查看:122
本文介绍了为使用数组,向量,结构等传递给可变函数或可变参数模板函数w / out的所有参数指定一个类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个函数(可能是成员函数,不是很重要...也许它吗?),需要接受未知数量的参数,但我想让他们是相同的类型。我知道我可以传入一个数组或向量,但我想能够直接接受args的列表,没有额外的结构,甚至额外的方括号。它看起来不像可变函数本身是类型安全的,我不知道如何去这个w /可变参数模板函数。这实际上是我的目标(更有可能不正确的代码,完全不是为了获得列表的龙,lol):

I'm creating a function (possibly member function, not that it matters... maybe it does?) that needs to accept an unknown number of arguments, but I want all of them to be the same type. I know I could pass in an array or vector, but I want to be able to accept the list of args directly without extra structure or even extra brackets. It doesn't look like variadic functions by themselves are typesafe, and I wasn't sure how to go about this w/ variadic template functions. Here's essentially what I'm aiming for (more than likely not correct code, and totally not for the purpose of getting lists of dragons, lol):

//typedef for dragon_list_t up here somewhere.

enum Maiden {
    Eunice
    , Beatrice
    , Una_Brow
    , Helga
    , Aida
};

dragon_list_t make_dragon_list(Maiden...) {
    //here be dragons
}

template<Maiden... Maidens> dragon_list_t make_dragon_list(Maidens...) {
    //here be dragons
}

USAGE

dragon_list_t dragons_to_slay
    = make_dragon_list(Maiden.Eunice, Maiden.Helga, Maiden.Aida)
;

尝试了一些类似于上面的东西,没有骰子。建议?我可能有明显的疏忽?我知道这可能不是一个巨大的事情做这个改为:

Tried a few things similar to the above already, no dice. Suggestions? Obvious oversights I may have made? I know it may not be a huge deal to do this instead:

dragon_list_t make_dragon_list(std::array<Maiden> maidens) {
    //here be dragons.
}
dragon_list_t dragons_to_slay
    = make_dragon_list({Maiden.Eunice, Maiden.Helga, Maiden.Aida})
;

但如果可能的话,我更希望第一个方法。

but I'd much rather be able to do it the first way if possible.

推荐答案

您可以接受可变参数模板的参数,并让typechecking在转换后检查有效性。

You can just accept the arguments by the variadic template and let typechecking check the validity later on when they are converted.

您可以在函数接口级别检查可兑换性,以使用重载解析来拒绝完全错误的参数,例如使用SFINAE

You can check convertibility on the function interface level though, to make use of overload resolution for rejecting outright wrong arguments for example, by using SFINAE

template<typename R, typename...> struct fst { typedef R type; };

template<typename ...Args>
typename fst<void, 
  typename enable_if<
    is_convertible<Args, ToType>::value
  >::type...
>::type 
f(Args...);

对于你的用例,如果你知道从 std :: array<> 添加到你的 dragon_list_t ,然后你已经解决了它虽然根据上面的第一个选项

For your use-case if you know the steps to go from an std::array<> to your dragon_list_t then you have already solved it though according to the first option above ("convert-later"):

template<typename ...Items>
dragon_list_t make_dragon_list(Items... maidens) {
    std::array<Maiden, sizeof...(Items)> arr = {{ maidens ... }};
    // here be dragons
}

is_convertible 方法,您有一个拒绝早期模板,也会重载解析参数,如果不适用,拒绝它们。

If you combine this with the above is_convertible approach you have a reject-early template that also does overload resolution on arguments and rejects them if not applicable.

这篇关于为使用数组,向量,结构等传递给可变函数或可变参数模板函数w / out的所有参数指定一个类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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