0

I'm using C++20.

template <typename T> struct Foo {};

template <typename T> struct Bar {
  constexpr operator Foo<T>() const { return {}; }
};

template <typename T> void foo(Foo<T>) {}
void bar(Foo<int>) {}

int main() {
  Bar<int> b;
  bar(b); // fine
  foo(b); // error
}

Godbolt link so you can test online: https://godbolt.org/z/vYjs5nMxh

The error

<source>: In function 'int main()':
<source>:13:6: error: no matching function for call to 'foo(Bar<int>&)'
   13 |   foo(b);
      |   ~~~^~~
<source>:7:28: note: candidate: 'template<class T> void foo(Foo<T>)'
    7 | template <typename T> void foo(Foo<T>) {}
      |                            ^~~
<source>:7:28: note:   template argument deduction/substitution failed:
<source>:13:6: note:   'Bar<int>' is not derived from 'Foo<T>'
   13 |   foo(b);
      |   ~~~^~~
Compiler returned: 1

Is there a way to get deduction to work without requiring inheritance?

Chris Elrod
  • 357
  • 2
  • 8

0 Answers0