是否必须在模板中指定typename? [英] Does typename have to be specified inside template?

查看:134
本文介绍了是否必须在模板中指定typename?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Clang中,似乎GCC不需要在模板中指定类型名。

It seems that GCC does not require a typename to be specified inside the template while Clang does.

请考虑以下代码段:

template<
    typename T,
    typename value_type = T::value_type
>
void func(T t) {}

当Clang报告时,以上代码在GCC 10.0.1上成功编译以下错误消息:

The code above compiles successfully on GCC 10.0.1 when Clang reports the following error message:

❯ clang++ -std=c++20 asdf.cpp
asdf.cpp:3:27: error: missing 'typename' prior to dependent type name 'T::value_type'
    typename value_type = T::value_type
                          ^~~~~~~~~~~~~
                          typename
1 error generated.

为什么会这样?

UPDATE 1:

如@Language Lawyer所述,关于 typename 的第一个怀疑是 P0634R3 当前不受Clang支持。

As noted by @Language Lawyer, the first suspicion about typename is a feature of P0634R3 not currently supported by Clang.

更新2:

我将分开讨论该问题,因为它包含两个不同的主题。

I'm going to separate this question as it contains two different topics.

有关详细信息,请参见编辑历史记录。

See edit history for more details.

推荐答案

C ++ 20无需在特定情况下为依赖模板名称指定 typename 显而易见,必须指定的是类型名。例如,已指定模板参数的默认值它是 typename

C++20 removes the need for specifying typename for dependent template names in specific cases where it's obvious that what is being specified must be a typename. Like, for example, a template parameter's default value when that parameter has already specified that it is a typename:


一个合格的名称被称为仅类型ID上下文,如果它出现在模板的类型参数的[...] 默认参数,[...]

Clang 尚未实现此功能,但是GCC已

Clang has not yet implemented this feature, but GCC has.

这篇关于是否必须在模板中指定typename?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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