How to do this? (Template selection?)

C

Clay_Culver

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.
 
V

Victor Bazarov

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;}

Undefined behaviour. All identifiers starting with an underscore in the
global scope are reserved.
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?).

"Better" in what way?
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; }

Dereferencing a void pointer is impossible in C++. And, no, it would not
make others ambiguous because ellipsis ('...') is the last argument type
in the matching sequence.
---

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?

You could declare the specialisation of 'funct' for all things allowed,
and then not define the disallowed function. The linker will complain
about the missing function.
I'm trying to make this easy
for someone using the function, even if it makes the implementation
more complex.

I can't say I understand the intentions, but if you have your 'internal'
functions declared only for the types you allow, there should be no big
deal to have the "generic" 'funct' to refer to the undeclared 'internal'
one, which will cause a compiler error when you try instantiating the
'funct<SomeClass>' for the class that you didn't allow.

V
 
C

Clay_Culver

Dereferencing a void pointer is impossible in C++. And, no, it would not
make others ambiguous because ellipsis ('...') is the last argument type
in the matching sequence.
Ah ok, actually that solves my problem right there. I can make this
work that way then. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top