in Education by
Here, the context of polymorphic is expecting 'Derived' from 'Base&. Given class P { }; class Q : public P { }; auto operator + (const P& p, int x) -> DYNAMIC_DECLTYPE(P) { DYNAMIC_DECLTYPE(P) p2(p); p2.func(x); return p2; } Is there a way to have DYNAMIC_DECLTYPE working? I want to use this form instead of template T operator + (const T& t, int x) or have a potentially long list of if (!strcmp(typeid(p).name(), typeid(derived()).name()) { ... } because the latter cannot be used to restrict T to P or subclasses thereof (prove me wrong, if possible). JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
What you are trying to do is in every sense of the word a template pattern: You have an unbounded family of return types with matching function argument types. This should simply be a straight template. If you want to restrict the permissible types, you should add some typetrait magic. Perhaps like this: #include template typename std::enable_if::value, T>::type operator+(T const & t, int x) { T s(t); s.func(x); return s; } (If func returns a reference, you can shortcut this to return T(t).func(x);.)

Related questions

0 votes
    #include struct A { bool f( int a ) { std::cout...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    The code below copies data from one vector into another. If the vectors are large then I guess this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    The code below copies data from one vector into another. If the vectors are large then I guess this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I would like to use CHOLMOD's GPU acceleration, and have found several simple examples on how to use ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I'm relatively new to C++, and I'm hoping someone can help me resolve an issue I'm having with unique_ptr and vectors. ... ) { useLegs(); } void Biped::useLegs() { std::cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am a beginner and I just need a bit of help on why I getline is showing an error: this is what I have so far ... payments[MAX_ITEMS]; ifstream iFile; if ( argc != 2 ) { cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am a beginner and I just need a bit of help on why I getline is showing an error: this is what I have so far ... payments[MAX_ITEMS]; ifstream iFile; if ( argc != 2 ) { cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    I am working on a collection of classes used for video playback and recording. I have one main class ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    I've been reading the legacy code,which invloves in the customized memory pooling system, then I found that ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    We see lot of sorting techniques like Merge, quick, Heap. Could you help me decide which of these ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    If you have an existing package that doesn't have an .Rproj file, you can use devtools for the use_rstudio ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Hi I am having problems with relation and database design. There are three tables called 'articles' , ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have following Comments-on-Models design (source: http://railscasts.com/episodes/154-polymorphic-association): ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
...