#include <cstdio> class builtin_pack { long v[4]; public: builtin_pack ( long v1, long v2, long v3, long v4 ) : v{v1, v2, v3, v4} {} void builtin_op() { printf ( "%lx,%lx,%lx,%lx\n", v[0], v[1], v[2], v[3] ); }; template<typename Func, typename... Targs> void builtin_apply ( Func f, Targs ... t ) { for ( int i = 0; i < 4; i++ ) { v[i] = f ( tv[i]... ); } } }; class pack : builtin_pack { public: pack ( long v1, long v2, long v3, long v4 ) : builtin_pack ( v1, v2, v3, v4 ) {} template<typename Func, typename... Targs> pack& apply ( Func f, Targs ... t ) { this->builtin_apply ( f, t... ); return *this; } void op() { this->builtin_op(); } }; int main() { pack p1{0xff, 0x0f, 0xf0, 0x06}, p2{0x0f00, 0xf000, 0x6700, 0xff00}; pack p3{0x12340000, 0x56780000, 0x45120000, 0xdead0000}; p3.apply ( [] ( long i, long j, long k )->long{return i | j | k;}, p1, p2, p3 ); p3.op(); return 0; }
This code compiles with an error:
main.cpp:17:24: error: cannot cast 'pack' to its private base class 'builtin_pack' v[i] = f ( tv[i]... ); ^ main.cpp:29:15: note: in instantiation of function template specialization 'builtin_pack::builtin_apply<(lambda at main.cpp:42:16), pack, pack, pack>' requested here this->builtin_apply ( f, t... ); ^ main.cpp:42:8: note: in instantiation of function template specialization 'pack::apply<(lambda at main.cpp:42:16), pack, pack, pack>' requested here p3.apply ( [] ( long i, long j, long k )->long{return i | j | k;}, p1, p2, p3 ); ^ main.cpp:22:14: note: implicitly declared private here class pack : builtin_pack ^~~~~~~~~~~~ main.cpp:17:26: error: 'v' is a private member of 'builtin_pack' v[i] = f ( tv[i]... ); ^ main.cpp:22:14: note: constrained by implicitly private inheritance here class pack : builtin_pack ^~~~~~~~~~~~ main.cpp:5:10: note: member is declared here long v[4]; ^ 2 errors generated.
What I want to do is implement a mapping method with a user-defined (lambda function) function (called "apply"). It works readily when there is no hierarchy of a private implementation, a general wrapper, so when the v array is only in the pack class, it compiles and runs as expected. However, it does not work when data is stored in an inherited private class.
The class structure is a private developer class along with the wrapper class, and in the middle I came across this error.
Did I use the wrong variation pattern? Or is there a workaround?
(Sorry for my bad expression, as I am new to C ++ and stackoverflow and a non-native speaker of the English language, and a change or suggestion of the question is welcome as long as the original intention is reserved!)