c++ - CRTP with derived class overloading the method -
i encountered problem crtp, can't have same method name (with different signature) in both base , derived class. example reproduce issue following :
template <class t> struct base { void foo(){} void bar(){ static_cast<t*>(this)->foo(1,1); } }; struct derived : public base<derived> { int foo(int a,int b){ return a+b;} }; int test() { derived d; d.bar(); // works d.foo(1,1); // works d.foo(); // compilation error }
i tried under various versions of gcc, clang , icc using godbolt
anybody explain @ work here ?
gcc output:
in function 'int test()': 22 : error: no matching function call 'derived::foo()' d.foo(); // doesn't work ^ 13 : note: candidate: int derived::foo(int, int) int foo(int a,int b){ return a+b;}
it has nothing crtp, derived::foo
shadows base::foo
, if had declared foo
in inner scope.
one way un-shadow base
version, [1]place a
using base<derived>::foo;
in derived
.
[1] full example @ (http://coliru.stacked-crooked.com/a/5dc7558e12babbe5).
Comments
Post a Comment