3

This question is partially motivated by this previous question.

I am aware that alias templates are not deduced, except, in c++20, for class template argument deduction. For this reason, this code does not compile

#include <iostream>

template <bool Bv>
struct A;

template <>
struct A<true> {
  void print() const { std::cout << "True" << std::endl; }
};

template <>
struct A<false> {
  void print() const { std::cout << "False" << std::endl; }
};

enum class Btypes : int { cat, dog, wolf };

template <Btypes bt>
using B = A<(bt == Btypes::dog) ? true : false>;

template <Btypes bt>
void callB(const B<bt>& b)
{
  b.print();
}

int main()
{
  B<Btypes::dog> b;
  callB(b); // error: no matching function for call to ‘callB(B<Btypes::dog>&)‘

  return 0;
}

This answer details some subtleties on how a deduction of alias templates could/or could not work. I am however interested in a simpler case of non-type template parameters, like in the code above. It seems to me reasonable that a template deduction could be possible without ambiguities, since the non-type template parameters involved can only take a finite amount of values.

Is there a way, within c++17, to circumvent the lack of template parameter deduction for a class like in the code above, with non-type template parameters? That is, to make the code legal without changing the definition/signature of the function callB (and keeping the alias)?

francesco
  • 7,189
  • 7
  • 22
  • 49

0 Answers0