473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

template based design

mjm
Folks,

I am encountering the following problem when using templates in
writing a library:
since template functions are only instantiated at the point of use
classes don't have to follow well defined interfaces anymore. When the
template is instantiated the compiler looks for all the pieces and if
it finds them all you have your function and if not you have a
compiler error.

I'll try to make this more concrete in the following example: I am
writing a library for option pricing. There are two approaches:

1. Monte Carlo simulation.

For this I need price paths and the option must define a method

Double payoffAlongCurrentPath(PathGenerator*);
2. Tree methods.

For this to work the option must define a method

Double payoffAtCurrentNode(Tree*);
Now I can go ahead and define standalone functions

namespace Pricing {

template<class PathGenerator, class Option>
Double monteCarloPrice(PathGenerator* thePathGenerator, Option*
theOption)
{
// generate some paths and process them....
Double h=theOption->payoffclassAlongCurrentPath(thePathGenerator);
//...............
}
template<class Tree*, class Option>
Double treePrice(Tree* theTree, Option* theOption)
{
// process the nodes in the tree...
Double h=theOption->payoffAtCurrentNode(theTree);
//...............
}

};

Now the class Option needs to implement neither of the two methods 1.
or 2.
If 1. is not implemented all that happens is that Monte Carlo pricing
won't work
and if 2. is not implemented treePricing won't work.

For some options there are no trees and for others there are no paths
so we can't have a uniform interface for the class Option.

If I provide such functions how is the user supposed to know which
methods the class Option has to implement in order to get certain
functionality.
I would have to define the class Option with trivial implementations
of 1. and 2. (maybe an error message that nothing is implemented) and
admonish the user to override these functions in a meaningful way in
order to make use of the pricing methods.

Is there a better way?
Jul 19 '05 #1
0 1484

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: arch | last post by:
Hi, I am trying to implement a class hierarchy where there will be a base class defining the interface and some common operations (interface+implentation): Constraints : no STL. This will be...
5
by: ian | last post by:
I have begun testing some code based on Chapter 1.5.1 of the book Modern C++ Design by Andrei Alexandrescu. The test code is listed below and the compiler error message that is generated is: ...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
7
by: Daniel Walzenbach | last post by:
Hello, I want to create a Word XML file based on the input users make in a VB.NET application. I imagine creating a template in Word and saving it as a XML file. I then want to fill the...
0
by: peter | last post by:
Hi All, I have a template with some VBA code behind and some controls on it such as combo boxes. When I try to create a new document based on this template from C# code it opens fine but the...
35
by: Steven T. Hatton | last post by:
Perhaps I'm just a bit frustrated, and I will soon realize the clear truth of the matter, but right now I have some serious misgivings about the value of investing a lot of time and effort into...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
10
by: Renato Golin | last post by:
Hi all, I'm fiddling with policies but I'm having some problems... My Code: class MovePolicy { }; class EatPolicy { }; class ReproducePolicy { };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.