模板参数含concept时GCC编译报错,哪个编译器合规?
模板模板参数的Concept约束兼容性问题
我想要编写一个以模板为参数的泛型结构体,要求这个内部模板仅接受符合std::regular concept的类型,编写的代码如下:
template<template<std::regular> typename T> struct example{}; template<template<std::regular> typename T> using example_t = example<T>;
这段代码在MSVC和Clang编译器中可以正常编译,但GCC会抛出错误,错误信息如下:
<source>:4:72: error: constraint mismatch at argument 1 in template parameter list for 'template<template<class> class requires regular< <template-parameter-2-1> > T> struct example' 4 | template<template<std::regular> typename T> using example_t = example<T>;; | ^ <source>:4:72: note: expected 'template<class> class requires regular< <template-parameter-2-1> > T' but got 'template<class> class requires regular< <template-parameter-2-1> > T'
请问哪个编译器的行为是正确的?
答案
根据C++20标准的规定,MSVC和Clang的行为是正确的,GCC的报错属于实现缺陷。
在C++20中,template<std::regular> typename T这种写法是合法的,它等价于显式声明一个带std::regular约束的模板参数:template<typename U> requires std::regular<U> typename T。example和example_t的模板模板参数约束完全一致,不存在不匹配的情况。
GCC出现这个错误是因为编译器在处理模板模板参数的concept约束匹配时存在逻辑问题,这类缺陷在GCC的后续版本中已经被修复,升级到较新的GCC版本即可解决该编译错误。
内容的提问来源于stack exchange,提问作者samw
相关产品推荐
相关产品推荐

