Jump to content

How to tie up function call with different return type and template parameter

- - - - -

  • Please log in to reply
4 replies to this topic

#1
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Hello,

I thought I knew a little something from boost libraries that would help
me create a array of the following mess, but I came up empty.
I like converting long stuff like this to arrays, better code you see.

Can anyone help me converting this scenario:
int none(void)
{
    for(size_t i=0; i<objects.size(); i++) {
        switch(type){
        case(ENUM_FA):
            cout << endl << endl << "Object "<< i <<  ", recieved value: " << objects[i]->getValue<int8_t>(strPropertyName);
            break;
        case(ENUM_GH):
            cout << endl << endl << "Object "<< i <<  ", recieved value: " << objects[i]->getValue<uint8_t>(strPropertyName); 
            break;
        case(ENUM_YA):
            cout << endl << endl << "Object "<< i <<  ", recieved value: " << objects[i]->getValue<int16_t>(strPropertyName); 
            break;
        }
    }
}
The template parameter also tells the return value. And I made this a lot shorter than it really is and made simplifications.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Not really clear what you want here. What do you mean by "tie up the template parameter?"
sudo rm -rf /

#3
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

dargueta said:

Not really clear what you want here. What do you mean by "tie up the template parameter?"

An example always clears things up.
Here's an equivalent scenario and a solution for it.
The function bad is the way I now have implemented it and
the function better is the tabled method in which I'd like to
solve the problem presented in the first post. The problem is
that I have no idea to take the template type and save it to
a table in such a way that I can simply loop over the table.

#include <iostream>

using namespace std;

int getValue(int param) { return param; }

void bad(int type)
{
    switch(type)
    {
    case(23):
        cout << "Result: " << getValue(99) << endl;
        break;
    case(25):
        cout << "Result: " << getValue(101) << endl;
        break;
    case(31):
        cout << "Result: " << getValue(13) << endl;
        break;
    }
}

void better(int type)
{
    static const int tabled[][2] = {
        {23, 99},
        {25, 101},
        {31, 13} };
    for(size_t i=0; i < sizeof(tabled) / sizeof(tabled[0]); ++i)
        if(type == tabled[i][0])
            cout << "Result: " << getValue(tabled[i][1]) << endl;
}

int main(int argc, char** argv)
{
    bad(23);
    bad(25);
    bad(31);

    better(23);
    better(25);
    better(31);

    return 0;
}




#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
And so by template you mean this?
template <typename T> void foo(T type)
{
    static const T tabled[][2] = { /* initialization */ };
    // the rest of the function
}

Or do you not mean a literal C++ template?
sudo rm -rf /

#5
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
In this case I'm calling a template function.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users