是否可以从括号类型初始化推断元组的模板参数? [英] Is it possible to infer template parameters of tuple from brace-type initialization?

查看:119
本文介绍了是否可以从括号类型初始化推断元组的模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中,是否可以允许扣除 tuple



<$的模板参数类型p $ p> #include< tuple>
#include< string>

template< class T1,class T2>
void fun(std :: tuple< T1,T2> t,std :: string other){}

int main(){
fun(std :: tuple< double,int>(2.,3),std :: string(other)); // ok
fun(std :: make_tuple(2.,3),std :: string(other)); //确定,但试图避免`make_tuple`
fun({2.,3},std :: string(other)); //所需的语法但
//给出编译错误:候选模板忽略:不能推断模板参数'T1'void fun(std :: tuple< T1,T2> t)
}

我添加了第二个参数 other ,以避免涉及variadic函数 fun 级别的参数。此外,我试图避免使用 make_tuple ,至少从用户代码(即在 main() )。事实上,只要允许期望的语法,就不需要 tuple 键入所涉及的元素,并且在稍后阶段可以推断其元素类型。



(此外,虽然类似,但它与 initializer_list 无关,因为它根本不工作



它至少与 clang 3.2 gcc 4.7失败.2 。有任何希望它将与当前或未来的标准工作吗? (例如未来(?) initializer_tuple 。)



(这对于增加函数调用的表达力非常有用,通过聚合子元素,但可以争论)






注意:对于示例代码,似乎 std :: forward_as_tuple std :: make_tuple 更合适,因此参数不一定要复制: http://en.cppreference.com/w/cpp/utility/tuple/forward_as_tuple

解决方案

不,绝对没有办法。如果元素类型不是相同类型,则扣除失败。如果参数不是 std :: initializer_list< T> ,则根本没有扣除(你是对的 initializer_list 与你给的大括号没有任何关系,但这是扣除工作的简单规则)。



模板参数值必须由涉及它们的其他函数参数位置推断或必须明确指定。


In this example, is it possible to allow the deduction of the template parameters type of the tuple?

#include<tuple>
#include<string>

template<class T1, class T2>
void fun(std::tuple<T1, T2> t, std::string other){}

int main(){
    fun(std::tuple<double, int>(2.,3), std::string("other")); // ok
    fun(std::make_tuple(2.,3), std::string("other")); // ok, but trying to avoid `make_tuple`
    fun({2.,3},std::string("other")); // desired syntax but
    // giving compilation error: candidate template ignored: couldn't infer template argument 'T1' void fun(std::tuple<T1, T2> t)
}

I added the second argument other to avoid solutions involving variadic arguments at the level of the function fun. Also, I am trying to avoid the use of make_tuple, at least from the user code (i.e. in main()). In fact it doesn't need to be the tuple type the one involved as long as the "desired syntax" is allowed and somehow its element types can be deduced at later stage.

(Also, although similar, this has nothing to do with initializer_list since it doesn't work at all having dissimilar elements in the braces)

It fails at least with clang 3.2 and gcc 4.7.2. Is there any hope that it would work with the current or a near-future standard? (e.g. a future(?) initializer_tuple.)

(This can be very useful to add expressiveness to function calls, by aggregating subelements, but that can be argued about)


Note: For the example code it seems that std::forward_as_tuple is more appropriate than std::make_tuple so the arguments are not necessarily copied: http://en.cppreference.com/w/cpp/utility/tuple/forward_as_tuple . Still not as nice as if there were a build-in language feature for heterogeneous initializer lists.

解决方案

No, there is absolutely no way. Deduction fails if the element types are not of the same type. And no deduction is done at all if the parameter is not a std::initializer_list<T> anyway (you are right that initializer_list doesn't have anything to do with the braces you give, but this is the simple rule for deduction to work).

The template parameter values must be deduced by other function parameter positions involving them or must be explicitly specified.

这篇关于是否可以从括号类型初始化推断元组的模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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