模板具有默认参数时,省略尖括号 [英] Omit angle brackets when template has default parameters

查看:286
本文介绍了模板具有默认参数时,省略尖括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个带有默认模板参数的类模板:

Suppose we have a class template with default template parameter:

template <typename T = int>
class Foo {};

在函数内部创建变量时,我们可以省略尖括号:

We can omit angle brackets when creating a variable inside a function:

int main()
{
    Foo a; // gets properly deduced as Foo<int>
}

但是我们不能对成员变量执行此操作:

But we can't do that for member variables:

struct S
{
    Foo a; // Deduce Foo<int>
};

我们不能有这样的派生类型:

We can't have derivative types such as this:

Foo* ptr; // Foo<int>*
Foo& ref; // Foo<int>&
int Foo::* mem_ptr; // int Foo<int>::*
std::function<Foo(const Foo&)> fn; // std::function<Foo<int>(const Foo<int>&)>

我们无法接受参数并返回它们:

We can't accept parameters and return them:

Foo Bar(const Foo&); // Foo<int> (*)(const Foo<int>&)

为什么?这是否被视为标准中的错误?有解决方案吗?省略尖括号是否有实际问题?

Why? Is this considered a bug in the standard? Is there a proposal to fix it? Are there any actual problems with omitting angle brackets?

我的用例:

我有一个提供默认参数的类模板. template参数是仅供专家使用的功能,我本人从未使用过,但是对于那些希望获得总体灵活性的专家来说,它是存在的.现在,对于其他99%的人,我想隐藏一个事实,即Foo实际上是一个类模板,但是它不起作用,因为用户在将其声明为成员变量时必须键入Foo<>,当前的解决方案是:

I have a class template which provides default argument. The template parameter is an expert-only feature that I myself never use but it is there for those 1% of experts who want that total flexibility. Now for other 99% I want to hide the fact that Foo is actually a class template but it doesn't work because users have to type Foo<> when declaring it as a member variable, current solution is this:

template <typename T = int>
class BasicFoo {};

using Foo = BasicFoo<>;

但是它使实现代码复杂化,而且一点也不优雅.

But it complicates implementation code and is not elegant at all.

推荐答案

这被认为是标准中的错误吗?

Is this considered a bug in the standard?

否.

模板是一个命名结构,它根据一组参数生成另一个结构(类/函数/变量).模板的名称不是它生成的构造的名称.模板的名称就是模板的名称;要命名模板生成的内容,必须提供模板参数.

Templates are a named construct which generates another construct (classes/functions/variables) based on a set of parameters. The name of a template is not the name of the construct which it generates. The name of a template is just the name of the template; to name the thing the template generates, you must provide the template parameters.

Foo是模板的名称; Foo<>是由该模板及其关联的模板参数生成的类的名称.

Foo is the name of a template; Foo<> is the name of a class generated by that template and its associated template parameters.

在一些地方,C ++允许使用模板,以便从表达式序列中推导出其参数.但是这些都是非常特殊的地方,为方便起见而创建.出于隐藏的目的而存在,它们不存在,即名称代表模板而不是生成的构造.

There are a couple of places where C++ allows a template to be used in such a way that its parameters are deduced from a sequence of expressions. But these are very specific places, created for convenience purposes. They do not exist for the purpose of hiding the fact that a name represents a template rather than the generated construct.

是否有解决方案?

Is there a proposal to fix it?

没有任何要修复的问题.目前还没有提议以这种方式添加更改.

There is nothing broken to fix. And there are at present no proposals adding changes in this way.

省略尖括号是否有实际问题?

Are there any actual problems with omitting angle brackets?

定义实际问题".从理论上讲,是否可以更改语言,以便如果默认所有模板参数,则可以在不使用模板参数的情况下使用模板名称,以同时表示模板模板生成的内容?

Define "actual problem". Is it theoretically possible to have the language altered so that, if all of a template's parameters are defaulted, the name of the template can be used without template parameters to simultaneously mean the template and the thing the template generates?

有可能 .但要指定复杂.您将需要一位认真的规范医生,他需要对C ++语法有深入的了解,才能确定是否可能,以及需要进行哪些确切的更改.

It is probably possible. But it would be complicated to specify. You would need a serious spec-doctor, one who understands the C++ grammar at a deep level, to know for sure whether it is possible, and what exactly would need to be changed to do it.

但最终,它仅对一小部分选择的模板有用:这些模板的所有参数均具有默认值.真正的问题是,这种情况是否足够普遍,值得付出努力.

But at the end of the day, it would only ever be useful for a small, select set of templates: templates that have default values for all of its parameters. The real question is whether that is a common enough case to be worth the effort.

这篇关于模板具有默认参数时,省略尖括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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