使用带有模板 class 的 c++20 概念的奇怪 GCC(主干)行为:它是错误还是功能?
Strange GCC (trunk) behaviour using c++20 concept with templated class: Is it a bug or a feature?
最近我在使用 concept
s 为 template
d class
定义不同的构造函数。这是代码:
#include <iostream>
#include <concepts>
template<typename T> concept scalar = std::is_scalar_v<T>;
template<typename T>
class Foo
{
public:
Foo(T t) requires scalar<T>: _t{t} { std::cout << "is scalar" << std::endl; }
Foo(T t) requires (not scalar<T>): _t{t} { std::cout << "is not scalar" << std::endl;}
private:
T _t;
};
class cls {};
int main()
{
Foo(true);
Foo('d');
Foo(3.14159);
cls c;
Foo(c);
return 0;
}
To my surprise the code does not compile using GCC (trunk).
错误消息的第一部分如下:
error: conflicting declaration 'Foo<...auto...> c'
您怎么看,这是错误还是功能?
括号和表达式似乎是一个没有任何其他内容的构造函数调用的问题(也许是最令人烦恼的解析类情况?)。
这些编译:
auto fc = Foo(c);
Foo{c};
Foo(cls{});
Foo<cls>({});
最近我在使用 concept
s 为 template
d class
定义不同的构造函数。这是代码:
#include <iostream>
#include <concepts>
template<typename T> concept scalar = std::is_scalar_v<T>;
template<typename T>
class Foo
{
public:
Foo(T t) requires scalar<T>: _t{t} { std::cout << "is scalar" << std::endl; }
Foo(T t) requires (not scalar<T>): _t{t} { std::cout << "is not scalar" << std::endl;}
private:
T _t;
};
class cls {};
int main()
{
Foo(true);
Foo('d');
Foo(3.14159);
cls c;
Foo(c);
return 0;
}
To my surprise the code does not compile using GCC (trunk). 错误消息的第一部分如下:
error: conflicting declaration 'Foo<...auto...> c'
您怎么看,这是错误还是功能?
括号和表达式似乎是一个没有任何其他内容的构造函数调用的问题(也许是最令人烦恼的解析类情况?)。
这些编译:
auto fc = Foo(c);
Foo{c};
Foo(cls{});
Foo<cls>({});