Thank you very much Jorg for your complete answer.

Best regards,
    Luca Ciucci.


 
Da "Jorg Brown" jorg.brown@gmail.com
A std-proposals@lists.isocpp.org
Cc "develop@lucaciucci99.com" develop@lucaciucci99.com
Data Mon, 9 Sep 2019 15:01:27 -0700
Oggetto Re: [std-proposals] Function partial template specialization

It took me a long time to really wrap my head around the fact that almost every time you think you need function template specialization, function overloading will give you what you need.

Part of that process was realizing that, once you have the power of function overloading, which you don't have with templated classes, it becomes a problem for function template specialization to realize precisely which function template you're specializing.

In any case, in the extremely rare cases where overloading doesn't work, you can definitely always make a functor, rather than a function, that works.

On top of that, ever since C++17, the power of "if constexpr", combined with a function return type of "auto", fills in any gray area you think might exist.

In your first example:

template <int N, class T>
void function(void) {
  T t;
  GeneralCode(N, &t);
}

template <class T>
void function<42, T>(void) {
  T t;
  CodeFortyTwo(&t);
}


You could also implement it using functors:

template <int N, class T>
struct functor {
  void operator()(void) const {
    T t;
    GeneralCode(N, &t);
  }
};
template <class T>
struct functor<42, T> {
  void operator()(void) const {
    T t;
    CodeFortyTwo(&t);
  }
};

template <int N, class T>
void function(void) {
  functor<N, T>{}();
}

Or with function overloads:

template <class T, int N>
void function_overload(std::integral_constant<int, N>) {
  T t;
  GeneralCode(N, &t);
}

template <class T>
void function_overload(std::integral_constant<int, 42>) {
  T t;
  CodeFortyTwo(&t);
}
template <int N, class T>
void function(void) {
  function_overload<T>(std::integral_constant<int, N>{});
}

(But note that typically you wouldn't need an extra overload, in particular if your function takes a type and you're using template argument deduction based on that type, overloading works very naturally and without the need to add a second function name.)

But with "if constexpr", this becomes quite trivial:

template <int N, class T>
void function(void) {
  T t;
  if constexpr(N == 42) {
    CodeFortyTwo(&t);
  } else {
    GeneralCode(N, &t);
  }
}


This brings me back to the end of your message, where you write:

> This could be very useful in some situation

The bar for adding a feature to C++ is much, much higher than this.  A typical proposal contains a line more like:

"This is extremely useful in this situation ..."
or
"This is very tedious with the current language standard, but becomes simple and clean after this proposal"


Which is to say, the committee will want to know much more about what situation would be helped, and will insist that more currently-available alternatives be considered, before discussing any additional language feature.

-- Jorg