【问题标题】:MSVC Alias Template Bug?MSVC 别名模板错误?
【发布时间】:2019-09-20 17:14:20
【问题描述】:

我正在将我之前为 clang 和 gcc 编写的库转换为 MSVC,我遇到了我认为必须是错误的问题,但我对标准的了解不够当然。

以下代码会产生关于未声明标识符和无效默认参数的错误:

template <class T>
struct dummy_struct {};
template <class T>
using dummy_alias = dummy_struct<T>;

template <template <class> class Thing>
struct foo {
    template <template <class> class T = Thing>
    void bar() {}
};

int main() {
    foo<dummy_alias> fdsa;
    fdsa.bar();
}
error C3202: 'Thing': invalid default argument, expected a class template
note: see reference to class template instantiation 'foo<dummy_alias>' being compiled
error C2065: 'Thing': undeclared identifier

这是上面示例的编译器资源管理器链接:https://godbolt.org/z/e2SEpD

问题的根源似乎是 MSVC 不习惯将别名模板用作模板模板参数。

我认为这一定是一个错误,但我认为在我提交报告之前,我应该由可能更熟悉该标准的人运行它。 提前致谢!

为了澄清,这段代码在最新版本的 gcc、clang 和 icc 上编译没有问题。

【问题讨论】:

  • 最好在问题中包含实际的错误消息。
  • 我已更新问题以包含错误消息,感谢您的建议!

标签: c++ visual-c++ compiler-errors language-lawyer


【解决方案1】:

通过严格阅读标准,程序格式错误,因此 gcc 和 clang 不打印诊断是错误的。

但是,我怀疑这样做的目的是应该可行,因此可能需要一份标准缺陷报告。

[temp.param]/3

type-parameter 其标识符不跟在省略号后面,将其 identifier 定义为 typedef-name(如果声明时没有 @987654326 @) 或 template-name(如果用 template 声明)在模板声明的范围内。

所以像Thing这样的模板模板参数是一个template-name,这使得写一个像Thing&lt;int&gt;这样的simple-template-id是有效的。

但是[temp.arg.template]/1

模板template-parametertemplate-argument应该是类模板或别名模板的名称,表示为id-expression em>。

(从技术上讲,默认模板参数在语法上不是模板参数,因为template-parameter syntax tree 在@ 之后不使用非终结符模板参数 987654330@ 令牌,而不是直接使用 type-idid-expression。但是,[temp.param]/11 使它们相关,因为 type-idid-expression 可以被解析为 template-argument:“默认的 template-argument 是一个 template-argument em> ([temp.arg]) 在 template-parameter= 之后指定。")

这里Thing 肯定不是类模板的名称,也没有说它是别名模板的名称,只是一个模板名称

【讨论】:

  • 这很有趣,@aschepler,非常感谢这里的详细分类。我有一种感觉,这可能非常微妙。我从来没有做过标准的缺陷报告,如果你认为这是合理的,我当然有兴趣这样做。您可以为此向我指出任何类型的文档吗?
  • 如果我理解正确,您是说通过严格阅读 Thing 是一个 template-name 但既不 命名类模板 也不 命名一个别名模板?在标准谈论 template-name referingnaming 特定类型的模板时,这不会导致更多问题吗? msvc 似乎也没有实现这个特定的读取,因为它应该禁止 Thing 在任何地方作为 template-argumentbut doesn't
  • @uneven_mark 好吧,拥有 template-name 的唯一方法是模板声明中的实际 id-expression,即名称类模板、函数模板、变量模板或别名模板;或者,模板模板参数。诚然,MSVC 显然不会按照这种阅读方式进行。似乎它只是碰巧在一个案例上失败了。
猜你喜欢
  • 1970-01-01
  • 2020-10-13
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 2015-05-02
相关资源
最近更新 更多