News for hackers. Stuff that matters.
Re:Final features of C++17 by Kristine1975 on Friday July 01, 2016 @10:55AM (#12016509)
>I need to assert in the base class that all my "derived" classes implement a method with a certain signature. Does C++ allow me to do this?

Of course (C++14, possibly C++11):

  #include <type_traits>
  
  template <typename T, typename... Params>
  struct HasFoo {
      // SFINAE will disable this function template if U has no method foo(float)
      template <typename U>
      static constexpr bool check(decltype(std::declval<U>().foo(std::declval<Params>()...))*)
      {
          // We don't check foo's return type here
          return true;
      }
      
      // Fallback if above function template is disabled
      template <typename U>
      static constexpr bool check(...)
      {
          return false;
      }
      
      static constexpr bool value = check<T>(0);
  };
  
  template <typename D>
  struct Base {
      Base()
      {
          static_assert(HasFoo<D, float>::value, "D must implement 'foo(float)'");
      }
  };
  
  struct Derived {
      void foo(int)
      {
      }
  };

Since this essentially simulates a method call, implicit conversions work, as does overloading "foo".

As you have to implement "HasFoo" for every method you check, a macro will come in handy, say MAKE_METHOD_CHECK(Foo, bool, float)

I added some comments, feel free to ask if something is not clear :-)

Re:Final features of C++17 by im3w1l on Friday July 01, 2016 @01:49PM (#12018187)
Nice! Now to make the idea of compile time virtual functions real, can you force derived classes to have foo_impl, and have an inlined function foo in the base class that calls foo_impl? What about grand children classes?
Re:Final features of C++17 by Kristine1975 on Friday July 01, 2016 @02:17PM (#12018435)
>can you force derived classes to have foo_impl, and have an inlined function foo in the base class that calls foo_impl?

If that's all you want just write:

  void foo()
  {
    static_cast<Derived&>(*this).foo_impl();
  }
When the derived class doesn't have any method "foo_impl" (or no "foo_impl" with a suitable signature), you will get a compile-time error.

No need for the static_assert, that was just because KKKKkkkk1 asked whether checking for the existence of a method at compile-time was possible.

Compile-time polymorphism is essentially duck typing: If it quacks like a duck you can treat it as one. If it doesn't quack like a duck, but you still treat it as one you get a compiler error.

Re:Final features of C++17 by im3w1l on Saturday July 02, 2016 @06:02AM (#12021994)
Doesn't you code assume that there is exactly one derived class, and that the base class knows the name of it?
Re:Final features of C++17 by Kristine1975 on Saturday July 02, 2016 @07:32AM (#12022153)
The base class must be a template with one parameter being the derived class:

  template <typename D>
  class Base {
    ...
  };
and then:

  class Derived: public Base<Derived> {
    ...
  };
This is called the https://en.wikipedia.org/wiki/Curiously_recurring_template_p...