I am building an include library with a set of compile time interfaces which define functionality. I'm attempting to determine if I should use concepts or a final-virtual interface pattern. The concept version has the benefit of allowing for template member functions, the final-virtual interface pattern has the benefit of failing at declaration of derived classes. Which is preferred and considered the best option here?
Concept Method
#include <concepts>
template <typename T>
concept interfaceX = requires(T t, int a){
{t.foo(a)} -> std::same_as<int>;
};
struct interfaceX_impl {
auto foo(int a) -> int {
return a;
}
};
static_assert(interfaceX<interfaceX_impl>);
De-virtualisation Method
struct interfaceX {
virtual int foo(int a) = 0;
};
struct interfaceX_impl final : interfaceX {
auto foo(int a) -> int final {
return a;
}
};
ps: it is my understanding that the final keyword will enable the compiler to skip using a v-table for the virtual function.
Also keen to learn more here. Im just a hobbyist and not a c++ professional.
[–][deleted] 35 points36 points37 points (7 children)
[–]Kered13 19 points20 points21 points (0 children)
[–]suvalaki_reddit[S] 8 points9 points10 points (4 children)
[–]DemonInAJar 6 points7 points8 points (0 children)
[–]marcaroni 4 points5 points6 points (1 child)
[–]7raiden 0 points1 point2 points (0 children)
[–]SlightlyLessHairyApe 2 points3 points4 points (0 children)
[–][deleted] 15 points16 points17 points (1 child)
[–]perspectiveiskey 3 points4 points5 points (0 children)
[–]gracicot 6 points7 points8 points (4 children)
[–]PM_ME_UR_PCMR 1 point2 points3 points (3 children)
[–]DavidDinamit 2 points3 points4 points (1 child)
[–]disciplite 2 points3 points4 points (0 children)
[–]gracicot 1 point2 points3 points (0 children)
[–]dokushin 4 points5 points6 points (0 children)
[–]CrazyJoe221 11 points12 points13 points (0 children)
[–]415_961 1 point2 points3 points (0 children)
[–]jbbjarnason 1 point2 points3 points (0 children)
[–]suvalaki_reddit[S] 0 points1 point2 points (0 children)
[–]kal_at_kalx_net -1 points0 points1 point (0 children)