C++ Logo

std-discussion

Advanced search

Re: Templatized virtual functions

From: Matthew Woehlke <mwoehlke.floss_at_[hidden]>
Date: Thu, 13 Jun 2019 12:34:46 -0400
On 27/04/2019 09.46, Ritwik Das via Std-Discussion wrote:
> I am not sure I understand what you mean by “class type is complete”.

Practically speaking, "before the '}' of the class definition". IOW, the
set of instantiations must be specified *inside* of the class
definition, and becomes fixed upon the '}' of the class definition.

I think what you really want isn't a virtual template, but a way to
define an overload set which takes one or more type-variable parameters
which "act like" template type parameters for the purpose of the
definition, but where the overload set is otherwise just a set of
non-template functions.

IOW, you want this (strawman syntax):

  <template restrict typename A, restrict typename B
   : for A -> {int, float}
   : for B -> {int, float}>
  void foo(A a, B b)
  {
    ...
  }

...to be equivalent to this:

  void foo(int a, int b)
  {
    ...
  }
  void foo(float a, int b)
  {
    ...
  }
  void foo(int a, float b)
  {
    ...
  }
  void foo(float a, float b)
  {
    ...
  }

This should also allow me to write e.g.:

  <template restrict typename T : for T -> {...}>
  virtual void foo(T) = 0;

This lets me give a single definition, like a template would, but I'm
not actually writing a template. It's just shorthand for a set of
overloads with "similar" definitions (where "similar" is defined as the
definitions being equivalent to template instantiations).

I think this could actually be useful, and may be worth pursuing.

-- 
Matthew

Received on 2019-06-13 11:36:38