【问题标题】:a non-type template parameter cannot have type [closed]非类型模板参数不能有类型[关闭]
【发布时间】:2020-05-21 17:31:17
【问题描述】:

对于以后遇到这个问题的人来说,最初的问题是:“如何从这个article 获得最简单的示例(点)以使用 GCCCLANG?”

  • Edit 1 显示了使用 CLANG 失败但使用 GCC 编译的最小可能代码(均使用 -std=c++2a)。
  • Edit 2 显示了添加的更多代码,这也破坏了 GCC。

文章的作者 (@BarryRevzin) 非常友好地评论了为什么这还行不通的原因,谢谢 Barry!

编辑 1:

下面的简化代码适用于 gcc 9.3.0,但不适用于 clang 10.0.0:

struct Point {                                                                                                                                                                               
   int x = 0;                                                                                                                                                                                
   int y = 0;                                                                                                                                                                                
};                                                                                                                                                                                           

template <Point> // ok in C++20                                                                                                                                                              
void takes_tmpl_point();                                                                                                                                                                     

int main()                                                                                                                                                                                   
{
    // EMPTY
}

编辑 2:

根据作者的说法,由于编译器有点落后于标准,原始代码目前还不能在 GCC 或 CLANG 上运行。原代码如下:

struct Point {                                                                                                                                                                               
   int x = 0;                                                                                                                                                                                
   int y = 0;                                                                                                                                                                                
};                                                                                                                                                                                           

template <Point> // ok in C++20                                                                                                                                                              
void takes_tmpl_point();                                                                                                                                                                     

int main()                                                                                                                                                                                   
{                                                                                                                                                                                            
   takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
}

这将导致在 GCC 9.3 上出现以下编译错误:

test.cpp: In function ‘int main()’:
test.cpp:11:35: error: no matching function for call to ‘takes_tmpl_point<{1, 2}>()’
   11 |    takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
      |                                   ^
test.cpp:7:6: note: candidate: ‘template<Point <anonymous> > void takes_tmpl_point()’
    7 | void takes_tmpl_point();
      |      ^~~~~~~~~~~~~~~~
test.cpp:7:6: note:   template argument deduction/substitution failed:
test.cpp:11:35: error: could not convert ‘{1, 2}’ from ‘<brace-enclosed initializer list>’ to ‘Point’
   11 |    takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
      |                                   ^

并且在 clang 10.0.0 上出现以下错误:

test.cpp:6:16: error: a non-type template parameter cannot have type 'Point'
template <Point> // ok in C++20
               ^
test.cpp:11:21: error: expected expression
   takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
                    ^
2 errors generated.

使用的编译器:

  • clang:clang 版本 10.0.0-4ubuntu1
  • gcc: gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0

【问题讨论】:

  • OK:您的编译器是否声称符合所有 C++20?
  • GCC 9.3 对你最近的例子没问题。

标签: c++ c++20


【解决方案1】:

Clang 还没有将类类型实现为非类型模板参数,请参阅 this table 中的 P1907。

gcc 确实实现了它们,但这里实际上存在一个问题。 template-argument 的语法实际上不允许 braced-init-list。这是一个明显的语言缺陷(在P1907 之前没有理由拥有这样的东西,但现在肯定没有理由拥有它)。目前这是一个语言错误。尽管如此,gcc 还是继续支持 braced-init-list 作为模板参数...只是不支持 designated-initializer-list

所以我的博客文章比实际语言领先了一点......直到语言赶上来,即使这在技术上不受支持:

takes_tmpl_point<{.x=1, .y=2}>(); 

这绝对是有效的:

takes_tmpl_point<Point{.x=1, .y=2}>();

【讨论】:

  • 谢谢巴里!因此,您的博客讨论了该语言“将”如何工作,但编译器却落后了,明白了!有没有办法让它在当前的限制下工作?我真的很喜欢这种风格...感谢您的回复!
  • @FernandoJeronymo 你不能takes_tmpl_point&lt;{.y=3}&gt;(); 但你可以takes_tmpl_point&lt;Point{.y=3}&gt;(); 你只需要命名类型。
  • 你的命名在这里有点反常 IMO:struct type_t { using type = T;。在 STL 中,_t 后缀是 ::type 成员的用法,而不是那些的容器。
  • @bipll 标准库中有 种类型以 _t 结尾:lock tag typesin place tag typesallocator tag typesentinel @987654328 @。更不用说[u]intN_tuintptr_t等整数类型了。
  • @Barry 标量或空,是的。
猜你喜欢
  • 2014-08-19
  • 1970-01-01
  • 1970-01-01
  • 2014-01-12
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
相关资源
最近更新 更多