c++ - How to let a base method be defined only if the derived class does not define a method of the same name -


I have a templated base class, which defines some kind of default functionality A templated derivative class overload if it wants to do this "default" functionality takes any argument as a fractional template function.

If a user fails to properly define the prototype of the uplifted class then the base class method will inadvertently be called. On the contrary, if the authors of the derived class want to apply a new prototype for an existing work, then the base class will become obsolete for that function name (this will not provide the usual "fallback" for other derivative methods, Do not choose to define prototypes).

So ... instead of the monster I give an example of what I am trying to do:

  template & lt; Typename derived & gt; Class Base {template & lt; Typename ... Args & gt; Zero Hook 1 (Constant Argus & amp; ...) // - & gt; Std :: enable_if (derivative :: hook1 does not exist) {// nothing} template & lt; Typename ... Args & gt; Zero Hook 2 (Cost Argus & amp; ...) // - & gt; Std :: enable_if (Derivative: hook 2 does not exist) {// do nothing} // ... hook 3, hook 4, etc.}; // This specific derived class is only the Octode 1 hook 1 category derived: For example, the public base & lt; DerivedExample & gt; {Template & lt; Typename SomeArg & gt; Zero Hook1 (Const SomeArg & arg) {// do something}};  

Obviously, those enable_if statements do not make sense, but they clarify the functionality that is as Is it worth achieving?

First you have to define some properties specifically to test the presence of a member function such as That:

  struct has_hook_one_impl {template  

This feature uses the expression SFINAE to detect the presence of a member, you can read more about the expression SFINAE.

After defining this feature, its use becomes simpler and you can interpret others who are similar to them. So in your case, enable_if will do this:

  template & lt; Typename derived & gt; Class Base {template & lt; Typename ... Args & gt; Auto hook 1 (Constant Argus and ...) - & gt; Typename std :: enable_if & lt ;; Has_hook_one & lt; Derived & gt; :: value & gt; :: type {// nothing}};  

Applying other meta functions that detect the presence of other member functions should be easy enough and the reader was left in the form of an exercise.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -