1

I have a function that generates a random number. The problem is that I use it in a template class where T may be int or string. When I choose string I get an error about impossible conversion. How can I return an integer and assign it to a string depending on the template type?

CTable(){
    for (int i = 0; i < 10; i++){
        int row = GenerateNumber();
        int col = GenerateNumber();
        T value = GenerateNumber(); //problem here when T is string
        CCellDescr c(row, col, value);
        cells.push_back(c);
    }
}

int GenerateNumber(){
    int number = rand() % 10 + 1;
    return number;
}
1
  • How can I define which one to choose depending on the type? Commented May 10, 2016 at 19:05

2 Answers 2

8

You could create a helper template to perform the conversions and specialize it on the T type. For example:

Example Code

#include <iostream>
#include <string>

int GenerateNumber()
{
    int number = rand() % 10 + 1;
    return number;
}

template<typename T>
T convert(int value)
{
    return value;
}

template<>
std::string convert(int value)
{
    return std::to_string(value);
}

template<typename T>
class CTable
{
public:
    CTable()
    {
        for (int i = 0; i < 10; i++)
        {
            int row = GenerateNumber();
            int col = GenerateNumber();
            T value = convert<T>(GenerateNumber());
            std::cout << "'" << value << "'\n";
        }
    }
};

int main()
{
    CTable<std::string> table;

    return 0;
}

Example Output

'8'
'6'
'10'
'8'
'4'
'7'
'2'
'10'
'3'
'6'

Live Example

Sign up to request clarification or add additional context in comments.

1 Comment

Fiend. Stop typing so fast!
0

You can also specialize the templated function to handle string differently

#include <cstdlib>
#include <string>

template<class T>
class CTable
{
public:
    CTable();
};

Removed the constructor's definition from the class. Normally I'm down on this because you gain more potential for screw-ups than benefits from separating template definition and implementation, but here it works.

int GenerateNumber()
{
    int number = rand() % 10 + 1;
    return number;
}

Generate number function is unchanged.

std::string randomStrings[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

template<>
CTable<std::string>::CTable()
{
    std::string value = randomStrings[GenerateNumber()]; 
}

Template constructor implementation for strings. This uses the random number as an index to an array of strings. Not super random, but it gets the point across.

template<class T>
CTable<T>::CTable()
{
    T value = GenerateNumber(); 
}

Template constructor implementation for everything else.

class foo
{

};

And a quickie class to demonstrate how to make this solution fall down.

int main()
{
    CTable<int> a;
    CTable<std::string> b;
    CTable<foo> c; // awwww shoot. Need protection from foos too. 
}

And a quickie main to try it all out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.