自C ++ 17以来,可否为构造函数提供模板参数推导,从而允许显式指定某些类模板参数? [英] Will template parameter deduction for constructors available since C++17 allow explicitly specify some of the class template arguments?

查看:170
本文介绍了自C ++ 17以来,可否为构造函数提供模板参数推导,从而允许显式指定某些类模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了最明显的构造函数模板参数推导用法之外,我还可以想象一些更复杂的用例,其中我们仅推导出模板类的部分参数,例如:

Apart from the most obvious usage of template parameter deduction for constructors, I can imagine some more complex use cases where we deduce only part of the parameters of the template class e.g.:

std::pair<int> p(1, 2); // std::pair<int, int>

尽管这种构造是函数中模板参数的推论的自然结果,但我找不到这种用法的任何示例. 也许是因为带有可变参数模板参数的类的模棱两可吗?

Although this construct would be natural consequence of the deduction of template parameters in functions I couldn't find any example of this kind of usage. Maybe it's because of the ambiguity in case of classes with variadic template arguments?

std::tuple<int> t(1, 2, 3); // std::tuple<int, int, int>

但是,这种方式引入的语法不会替代太好的"make_ *"包装器(请参见

However this way introduced syntax wouldn't substitute too good "make_*" wrappers (cf. N3602) where the functionality is available for us...

推荐答案

显然不是. P0091 的介绍声称确实可以,但是实际的措辞却大不相同:

Apparently not. P0091's introduction claims that it does, but the actual wording says quite differently:

与类模板相对应的模板名称,后跟带括号的表达式列表...

A template-name corresponding to a class template followed by a parenthesized expression-list...

这排除了模板名称+一些参数和带括号的表达式列表"的使用.措词的其他部分也从语法上予以排除.

This rules out the use of a template-name + some arguments and a "parenthesized expression-list". It is also ruled out grammatically in other parts of the wording.

因此,不可以在推导其他参数的同时指定一些参数.

So no, it won't be possible to specify some arguments while deducing others.

这篇关于自C ++ 17以来,可否为构造函数提供模板参数推导,从而允许显式指定某些类模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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