c++20 concepts constructor overloading

1000 views Asked by At

Let

#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() {}

When I try to compile it with gcc v.10.2 I obtain the following error:

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>&&’

but concepts in C++20 don't prevent instantiating functions that don't satisfy their constraints?. I know that SFINAE doesn't work because this costructor is not template, but concepts should work also with normal functions.

What I am doing wrong?

1

There are 1 answers

0
desio On BEST ANSWER

Ok it's a bug of gcc 10.2, I need to update to at least 10.3