C++20概念构造函数重载
c++20 concepts constructor overloading
让
#include<concepts>
struct B{};
template<typename T>
struct A{
A(int) {}
};
template<>
struct A<B>{
A(B d) {}
};
template<typename T>
struct C{
A<T> a;
C(B b) requires std::same_as<T, B> : a{b} {}
C(int i) requires (!std::same_as<T, B>) : a{i} {}
};
template class C<int>;
int main() {}
当我尝试使用 gcc v.10.2 编译它时,出现以下错误:
main.cpp: In instantiation of ‘C<T>::C(B) requires same_as<T, B> [with T = int]’:
main.cpp:22:16: required from here
main.cpp:18:45: error: no matching function for call to ‘A<int>::A(<brace-enclosed initializer list>)’
18 | C(B b) requires std::same_as<T, B> : a{b} {}
| ^
main.cpp:7:3: note: candidate: ‘A<T>::A(int) [with T = int]’
7 | A(int) {}
| ^
main.cpp:7:5: note: no known conversion for argument 1 from ‘B’ to ‘int’
7 | A(int) {}
| ^~~
main.cpp:6:8: note: candidate: ‘constexpr A<int>::A(const A<int>&)’
6 | struct A{
| ^
main.cpp:6:8: note: no known conversion for argument 1 from ‘B’ to ‘const A<int>&’
main.cpp:6:8: note: candidate: ‘constexpr A<int>::A(A<int>&&)’
main.cpp:6:8: note: no known conversion for argument 1 from ‘B’ to ‘A<int>&&’
但 C++20 中的概念不会阻止实例化不满足其约束的函数?。我知道 SFINAE 不起作用,因为这个构造函数不是模板,但概念应该也适用于普通函数。
我做错了什么?
好的,这是 gcc 10.2 的一个错误,我需要至少更新到 10.3
让
#include<concepts>
struct B{};
template<typename T>
struct A{
A(int) {}
};
template<>
struct A<B>{
A(B d) {}
};
template<typename T>
struct C{
A<T> a;
C(B b) requires std::same_as<T, B> : a{b} {}
C(int i) requires (!std::same_as<T, B>) : a{i} {}
};
template class C<int>;
int main() {}
当我尝试使用 gcc v.10.2 编译它时,出现以下错误:
main.cpp: In instantiation of ‘C<T>::C(B) requires same_as<T, B> [with T = int]’:
main.cpp:22:16: required from here
main.cpp:18:45: error: no matching function for call to ‘A<int>::A(<brace-enclosed initializer list>)’
18 | C(B b) requires std::same_as<T, B> : a{b} {}
| ^
main.cpp:7:3: note: candidate: ‘A<T>::A(int) [with T = int]’
7 | A(int) {}
| ^
main.cpp:7:5: note: no known conversion for argument 1 from ‘B’ to ‘int’
7 | A(int) {}
| ^~~
main.cpp:6:8: note: candidate: ‘constexpr A<int>::A(const A<int>&)’
6 | struct A{
| ^
main.cpp:6:8: note: no known conversion for argument 1 from ‘B’ to ‘const A<int>&’
main.cpp:6:8: note: candidate: ‘constexpr A<int>::A(A<int>&&)’
main.cpp:6:8: note: no known conversion for argument 1 from ‘B’ to ‘A<int>&&’
但 C++20 中的概念不会阻止实例化不满足其约束的函数?。我知道 SFINAE 不起作用,因为这个构造函数不是模板,但概念应该也适用于普通函数。
我做错了什么?
好的,这是 gcc 10.2 的一个错误,我需要至少更新到 10.3