473,322 Members | 1,425 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,322 software developers and data experts.

How to do this? (Template selection?)

I have a function which needs to return different values but that takes
no arguments, this is the function:

template <class T>
T funct();

For each type T for different types, there are different ways of
calculating the return value. To implement this, I've done the
following (for example):

void _internal_funct(int &val) {val = 42;}
void _internal_funct(double &val) {val = 3.141592;}

template <class T>
T funct()
{
T val;
_internal_funct(val);
return val;
}

This works reasonably well (though does anyone know a better way to do
this?). My only problem is I want to have a default return value for
anything I do not explicitly define (such as returning 0 for all
pointer types, and a compiler error for classes).

If I used a pointer instead of a reference I might have been able to
use an elipse, but it would make the first two definitions of
_internal_funct ambiguous:

void _internal_funct(...) { *[get a void pointer] = 0; }

---

More specifically, my ultimate goal is something like this:
// calculates a value and returns it based off of internalStateObject1
int i = funct<int>(internalStateObject1);

// calculates a bool and returns it based off of internalStateObject2
bool b = funct<bool>(internalStateObject2); // calculates a bool and
returns it

// calculates a pointer to a SomeClass object based off of
internalStateObject3
SomeClass *class = funct<SomeClass *>(internalStateObject3);

// compiler error or undefined behavior
SomeClass class2 = funct<SomeClass>(internalStateObject4);

Is this even possible in C++, or should I try to do this with something
other than template magic? I'm trying to make this easy for someone
using the function, even if it makes the implementation more complex.

Nov 22 '05 #1
0 1004

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

Similar topics

10
by: NKOBAYE027 | last post by:
my friends, the end()... hehehe no...nothing so dire... I'm at an impasse I think. I've created the MathematicalSet class as follows - it has 4 lists. An ObjectPool of the actual objects, a...
1
by: bob | last post by:
I asked a question yesterday about how I would parse a Word document using functions in some Interop.Word COM. Some told me to run a macro in Word to see how it works. Most of the code was......
1
by: Todd Fisher | last post by:
I need help please... I am try to capture the selectedindexchanged on a dropdownlist in a datalist template. I want to update another dropdown in the template based off of my first dropdown...
5
by: Paul | last post by:
Hi I need to change the text value of a dropdown list box that is in a template column of a data grid but was not sure how to access it, probably something like datagrid1.control.dropdownlist.text...
1
by: ven | last post by:
HELLO i have datagrid with template column <asp:TemplateColumn HeaderText="nazwa"> <HeaderStyle HorizontalAlign="Center"></HeaderStyle> <ItemStyle HorizontalAlign="Left"></ItemStyle>...
4
by: lizhuo | last post by:
hi all: I reading "C++ Templates: The Complete Guide " Part II: Templates in Depth he write: template<typename T> class B { public: enumE{e1=6,e2=28,e3=496}; virtual void zero(E e = e1);
3
by: andrej | last post by:
Hi, I'm working on a project in VB6 where I want to use something like templates saved in my database with customization of the template like selection of words and in specific fields to fill with...
14
by: Stan Brown | last post by:
I posted over the holidays, but I venture to repost since there were no responses. Maybe people overlooked my article among all the excitement. I'm looking for a template or model for menus that...
3
by: Auslander | last post by:
I'm new to VBA, and trying to learn how to write some code to open a word template from access and populate it using Document Properties. Here's my code: Private Sub OpenWindows_Click() Dim...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.