Function Pointer Template Argumentative Essay

Metaprgramming has become more and more popular and is now an essential part of modern C++ programming. By utilizing type information, it is possible implement generic programming and perform some compile time optimizations and checks. In this article, I use template metaprogramming to perform type extraction on a function and show an application of this technique.

To demonstrate one of the possible practical applications, I've created an open-source app that functions like a basic command interpreter or REPL. A new command is registered by providing a function pointer. The command class then has enough information to evaluate an input string, parse input parameters (check their number and types, convert if needed), invoke a command, and provide a result.

Here is an excerpt from the code that registers a new command; in this case, an function:

int f_add(int, int); dconsole::BaseCmd* pcmd = dconsole::createCmd("add", "an addition function", f_add);

(Other examples can be found in the linked files in ) After entering the aforementioned code into the command line, if you try to invoke the next string: , the command interpreter automatically parses it, performs the addition, and outputs a result. In the event you entered invalid parameters, it will tell you what is wrong:

input:

output:

Figure 1 a screenshot of my test app.


Figure 1: A REPL implementation showing argument type checking occurring in real time.

Implementation

In this implementation, I will use the concept of type lists introduced by Andrei Alexandrescu.

Here is the basic class to implement type lists:

template <class T, class U> struct TypeList { typedef T Head; typedef U Tail; };

To create a type list, I use this class this way:

typedef TypeList< int, TypeList<float, TypeList<char, NullType> > > MyTypeList;

The is a special, so-called "terminator" type. It helps to find the end of a list and can be simply defined as class .

Now, assuming that I can easily create a type list that enumerates all my function types (return type and input parameters), I could perform an automatic type check and conversion of data received as a string. So, if the parameters are passed as strings (as in the REPL), they need some method to check them automatically. What is the easiest way of doing this?

I suppose having something like this would be the easiest:

bool foo(int, int); Cmd* pcmd = createCmd(foo);

Now using this class, I can handle queries like this:

The class itself will check the parameters and perform a call if the parameters are correct. Even though in this article I am not discussing the operation of parsing string and converting it to respective types, you can see an example of it in my sample program, which demonstrates an application of the described technique by implementing a debug console for the application.

Now, let's see how to implement to automatically create a type list. In order to implement this function, I will use templates. Here is the declaration of the function:

// first parameter - command name, second - function type; template <typename R, typename T> CmdBase* createCmd(const std::string& name, T );

Here, the name is passed as a first parameter followed by pointer to the function. Implementation of this function is straightforward: It is used as a wrapper for the actual class that will do all the work.

Here is the implementation for a single-parameter version ( can also be void for functions with no parameters). I just pass all parameters to the class.


template <typename R, typename T> CmdBase* createCmd(const std::string& name, R (*fffp)(T)) { typedef void (*fffp_t)(T); return cmdCreator<fffp_t>::createCmd(name, fffp); }

Implementation for two or more partameters looks pretty similar.:

template <typename R, typename T1, typename T2> CmdBase* createCmd(const std::string& name, R (*fffp)(T1, T2)) { typedef R (*fffp_t)(T1, T2); return cmdCreator<fffp_t>::createCmd(name, fffp); }

Now let's proceed to the class.

Declaration looks very simple:

Here is the implementation for the function with one or none () input parameters:

template <typename R, typename T> struct cmdCreator< R (*)(T) > { static CmdBase* createCmd(const std::string& name, R (*fffp)(T)) { return new TypeList< R, TypeList<T, NullType> > (name, fffp); } };

As you can see, the static function is the heart of our system. It creates type list in a correct way based on templates parameters. For demonstration purposes, here is the implementation for functions with two parameters:

template <typename R, typename T1, typename T2> struct cmdCreator< R (*)(T1, T2) > { static CmdBase* createCmd(const std::string& name, R (*fffp)(T1, T2)) { return new TypeList< R, TypeList<T1, TypeList<T2, NullType> > > (name, fffp); } };

Notice that I create class while returning pointer to . As I will show later, I will derive the class from to benefit from polymorphism.

Now I have a way to create the correct , but how can I apply this?

Let's consider the earlier example and first extend to add automatic type checking.

For simplicity, let's assume that we have already parsed the input string and created a list of parameters, which are wrapped in a class. The class is a useful concept. For additional info you may check out or any other implementation. Here is how I implement type checking:

template <class T, class U> struct TypeList: public CmdBase { //... enum { myIndex = NumEl< TypeList<Head, Tail> >::value }; bool check(const std::vector< Variant >& params) { if(params.size() < myIndex) return false; return checkParams(params, 0); } bool static checkParams(const std::vector< Variant >& params, int offset = 0) { if(!params[offset].is<Tail::Head>()) return false; return Tail::checkParams(params, offset+1); } };

I also have to define a specialization for class, because we want the recursion in the above function to end.

template <class R> struct TypeList< R, NullType>: public CmdBase { enum { myIndex = NumEl< TypeList<Head, Tail> >::value }; //... void check(const std::vector< Variant >& params) { // same as above } bool static checkParams(const std::vector< Variant >& params, int offset = 0) { return true; } };

I introduced one more helper template class, , which keeps the counts for the type index in the type list. The declaration look like this: 



template < class TypeList> struct NumEl;

Here is the implementation:

//Specialization for terminator class template <class T> struct NumEl<TypeList<T, NullType> > { enum { value = 0 }; };

As you can see, it uses recursion for its type:

template <class Head, class Tail> struct NumEl<TypeList<Head, Tail> > { private: enum { temp = NumEl<Tail>::value }; public: enum { value = 1 + temp }; };

Now let's implement the method. I will also use the helper template class for which I will use same technique as for the class. But first, I'll provide a method. All that it does is call method, providing correct specialization.

virtual void run(const std::vector<Variant>& params) { if( check(params) ) executor<myfp, myIndex>::run(m_fptr_, params); }

The executor class is declared as:

The first template parameter is a function pointer that is passed by the class. The second template parameter is the number of function types. As you see, I use the compile-time constant, which was calculated with the help of ourhelper class.

The implementation for two parameters is:

template <typename T> struct executor<T, 2> { static void run(T fp, const std::vector<Variant>& vec) { fp( vec[0], vec[1] ); } };

Implementation for any other number of parameters is pretty straightforward.

As you can see, I use , which is the pointer to the function. But how do I get its type? In , I pass function pointer as second parameter of . Our type list does not have any template parameter for it, so how can we construct it? For this I use a trick also based on templates.

Guide to Perfect 6.0 AWA GMAT Score

Related AWA Resources:


I took the GMAT twice and scored 6.0 each time. I did put a lot of time in it the first time....too much actually. Being a non-native speaker and having not written a damn essay (of any kind) in many many years, I was very scared of the AWA. So, I went through every guide that I could find and wrote nearly 25-30 essays. Even had a friend grade them for me.....Pathetic, huh?

Anyway, for my second time, I just looked over my templates I created and wrote one of each the day before test just to refresh my memory on faster typing without making too many typos......

So, here it is....Enjoy, and please do not blame me if the 6.0 percentile goes down to 80 soon



AWA GUIDE

by Chineseburned

1. General Structure



Intro - Restate argument, point out flaws or state intention to discuss them below
1st Para - First,...
2nd Para - Second/In addition,...
3rd Para - Third/Finally,...
Conclusion - The argument is flawed/weak/unconvincing because of the above -mentioned...Ultimately, the argument can be strengthened if/by...


2. Structural Word (should be all over the essays)



  1. Supporting examples - for example, to illustrate, for instance, because, specifically
  2. Additional support - furthermore, in addition, similarly, just as, also, as a result, moreover
  3. Importance - surely, truly, undoubtedly, clearly, in fact, most importantly
  4. Contrast - on the contrary, yet, despite, rather, instead, however, although, while
  5. Decide against - one cannot deny that, it could be argued that, granted, admittedly
  6. Ying-yang - on the one hand/on the other hand
  7. Concluding - therefore, in summary, consequently, hence, in conclusion, ultimately, in closing


3. Templates



Intro:
The argument claims that ....(restate)
Stated in this way the argument:
a) manipulates facts and conveys a distorted view of the situation
b) reveals examples of leap of faith, poor reasoning and ill-defined terminology
c) fails to mention several key factors, on the basis of which it could be evaluated
The conclusion of the argument relies on assumptions for which there is no clear evidence. Hence, the argument is weak/unconvincing and has several flaws.

1st Para:
First, the argument readily assumes that......
This statement is a stretch....
For example,...
Clearly,...
The argument could have been much clearer if it explicitly stated that...

2nd Para:
Second, the argument claims that....
This is again a very weak and unsupported claim as the argument does not demonstrate any correlation between....and...
To illustrate,...
While,...
However,....indeed....
In fact, it is not at all clear...rather....
If the argument had provided evidence that.....then the argument would have been a lot more convincing.

3rd Para:
Finally,...
(pose some questions for the argument).....Without convincing answers to these questions, one is left with the impression that the claim is more of a wishful thinking rather than substantive evidence.

Conclusion:
In conclusion, the argument is flawed for the above-mentioned reasons and is therefore unconvincing. It could be considerably strengthened if the author clearly mentioned all the relevant facts....
In order to assess the merits of a certain situation/decision, it is essential to have full knowledge of all contributing factors. In this particular case....
Without this information, the argument remains unsubstantiated and open to debate.

4. Going from the templates to full-fledged essays




ESSAY QUESTION:
The following appeared in the editorial section of a national news magazine:[/b]

"The rating system for electronic games is similar to the movie rating system in that it provides consumers with a quick reference so that they can determine if the subject matter and contents are appropriate. This electronic game rating system is not working because it is self regulated and the fines for violating the rating system are nominal. As a result an independent body should oversee the game industry and companies that knowingly violate the rating system should be prohibited from releasing a game for two years."

Discuss how well reasoned you find this argument. Point out flaws in the argument's logic and analyze the argument's underlying assumptions. In addition, evaluate how supporting evidence is used and what evidence might counter the argument's conclusion. You may also discuss what additional evidence could be used to strengthen the argument or what changes would make the argument more logically sound.

YOUR RESPONSE:

Quote:

The argument claims that the electronic games rating system, although similar to the movie rating system, is not working because it is self regulated and violation fines are nominal, Hence, the gaming rating system should be overseen by an independent body. Stated in this way the argument fails to mention several key factors, on the basis of which it could be evaluated. The conclusion relies on assumptions, for which there is no clear evidence. Therefore, the argument is rather weak, unconvincing, and has several flaws.

First, the argument readily assumes that because the electronic game rating system is self regulated, it is not working well. This statement is a stretch and not substantiated in any way. There are numerous examples in other areas of business or commerce, where the entities are self regulated and rather successful. For instance, FIA, the Formula1 racing organization is self regulated. Yet, the sport is very popular and successful, drawing millions of spectators around the world each year. Tickets are rather expensive, races are shown on pay-per-view, and nearly all drivers are paid very well. Another example is the paralleled movie rating system that the argument mentions. The author fails to clarify whether it is working well, but it is clear that the movie rating system is pretty well received by people, who often base their decisions to go see a movie with kids or not on the movie rating. It has never been a case when someone would feel cheated by the movie rating and express disappointment afterwards. Since the movie rating system is also self regulated, it follows that this regulatory method is working pretty well and it is not obvious how it can be the reason for the poor electronic game rating system. The argument would have been much clearer if it explicitly gave examples of how the self regulatory system led to bad ratings and customer dissatisfaction.

Second, the argument claims that any violation fees for bad electronic game ratings are nominal. It thus suggests that this is yet another reason for the rating system not working. This is again a very weak and unsupported claim as the argument does not demonstrate any correlation between the monetary amount of the fines and the quality of the electronic game rating system. In fact, the argument does not even draw a parallel with the mentioned movie rating system and its violation fines. If any such correlation had been shown for the movie rating system, which supposedly works well, then the author would have sounded a bit more convincing. In addition, if the argument provided evidence that low violation fines lead to electronic game manufacturers to ignore any regulations with respect to the game rating system, the argument could have been strengthened even further.

Finally, the argument concludes that an independent body should oversee the game industry and companies that violate the rating system, should be punished. From this statement again, it is not at all clear how an independent regulatory body can do a better job than a self regulated one. Without supporting evidence and examples from other businesses where independent regulatory bodies have done a great job, one is left with the impression that the claim is more of a wishful thinking rather than substantive evidence. As a result, this conclusion has no legs to stand on.

In summary, the argument is flawed and therefore unconvincing. It could be considerably strengthened if the author clearly mentioned all the relevant facts. In order to assess the merits of a certain situation, it is essential to have full knowledge of all contributing factors.



5. Final tips



  • During the tutorial type in a few sentences in the mock essay window to get used to the keyboard.
  • Again during the tutorial, jot down on your notebook the basic structure of your essays or the opening sentences in case you get too nervous and forget them when the clock starts ticking.
  • Write as much as you can. Try to write at least 500 words per essay.
  • Always have the e-rater in mind as your potential reviewer. Remember that the human rater will make every effort to grade just like the e-rater. In that sense, keep your structure and volume in mind over actual quality/content.
  • Be careful of spelling mistakes. Double check words that you normally know you misspell (e.g. exercise). Try to finish 2-3 minutes before time is up so you can slowly re-read your essay for the purposes of spell checking. Do not reorganize/delete sentences/paragraphs with less than 2 min left.
  • No matter how great you thought your essays went, try to stay humble and focused - remember this was just a warm-up and the real stuff hasn't started yet!

Good luck!

Attachment:


AWA6.png [ 94.43 KiB | Viewed 101617 times ]

_________________

Chinese Democracy is misunderstood...at your nearest BestBuy.

Best AWA guide here: http://gmatclub.com/forum/how-to-get-6-0-awa-my-guide-64327.html


Last edited by bb on 14 Nov 2017, 21:18, edited 10 times in total.

Added the template as image

Categories: 1

0 Replies to “Function Pointer Template Argumentative Essay”

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *