什么是空白模板参数<>同时创建一个对象? [英] What is an empty template argument <> while creating an object?
问题描述
以下是一些有效的语法:
Here is some valid syntax:
std::uniform_real_distribution<> randomizer(0, 100);
它是如何工作的,它是否自动推导出对象模板?为什么需要在类型的末尾写入<>
?我不能删除<>
,它会一样吗?
How does it work, does it automatically deduce the object template? Why is it necessary to write <>
at the end of the type? Can I not remove the <>
and it will be the same?
推荐答案
通常,当第一个和后续的或仅有的参数具有默认模板参数(如果是整数,则为类型或值),可以使用和工作。另一种情况是当有一个模板参数包并且它是空的。
Typically this can be used and works when the first and succeeding, or only, parameter has a default template argument (type or value if it is an integral). An additional case is when there is a template argument pack and it is empty.
<>
仍然需要将其标识为模板类型。
The <>
is still needed to identify it as a template type.
在此 case类型声明为;
template <class RealType = double>
class uniform_real_distribution;
因此,默认的 RealType
uniform_real_distribution
是 double
。它等于 std :: uniform_real_distribution< double>
。
Hence the default RealType
for the template class uniform_real_distribution
is double
. It amounts to std::uniform_real_distribution<double>
.
关于 C ++ WD n4527 ,§14.3/ 4 (模板参数)
With reference to the C++ WD n4527, §14.3/4 (Template arguments)
使用模板参数包或默认模板参数时, - 纪录清单可以为空。在这种情况下,空的
<>
括号仍然用作模板参数列表。 [示例:
When template argument packs or default template-arguments are used, a template-argument list can be empty. In that case the empty
<>
brackets shall still be used as the template-argument-list. [ Example:
template<class T = char> class String;
String<>* p; // OK: String<char>
String* q; // syntax error
template<class ... Elements> class Tuple;
Tuple<>* t; // OK: Elements is empty
Tuple* u; // syntax error
- 结束示例 $ b
这篇关于什么是空白模板参数<>同时创建一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!