how can I convert a variable of type template to an integer??

You can't. templates take on a data type when they are declared. For example `vector<int> list;` is a vector of integers. Once declared it can not be changed to a vector of strings or something else.

But then again, maybe I misunderstood the question. Post an example of what you want to do

All I want is to convert the elem to an ASCII code
and elem maybe of any data type

``````template<typename T>
int h(T elem,int size){
int i;

int n=(int)elem
i=n%size;
return i;
}``````

That makes no sense. If T is type double how is the function supposed to convert the double to a single ascii character???

That makes no sense. If T is type double how is the function supposed to convert the double to a single ascii character???

Um, presumably by taking the modulo of the integer part against parameter `size` , which would presumably be 256 in this case? ;)

Not saying it makes much sense, still, but it should "work"....

And to the OP, you already have code, what seems to be the problem?

>> which would presumably be 256 in this case?
why 256? sizeof(double) != 256, so where did you get that number?

@OP you want this I think:

``````template<typename R, typename T>
R cast(const T& input){
stringstream stream;
stream << input;
R ret = R();
stream >> ret;
return ret;
}
int main(){
int i = cast<int>("12345");
float pi = cast<int>("3.1415");
}``````

>> which would presumably be 256 in this case?
why 256? sizeof(double) != 256, so where did you get that number?

It has nothing to do with the magnitude of the double, or the memory allocated to store it. I'm assuming the OP is taking the modulo of (int)(original_value) % size to limit the range of output values. 256 would be the max-value-plus-one of an 8-bit ASCII value the OP was hoping to convert to.... Or he could use 128 instead to get a 7-bit ASCII result. For what it's worth....

Minimally related, the integer portions of large float/double values can't be represented by the int type. My test here returns INT_MIN (from limits.h) for `float f = 3e12; int i = int(f);`

Thanks all, but I'm still have the same problem

I'm implementing now hash table, and I asked this question to get the has function for each element in file.
and to get the hash function, I must get the a specific number for each element and then divides it by n.

so How can I get this number.

any Help will be appreciated.

Here's an example of how you can split the key in bytes and then use them
to calculate the hash value, provided that the key is a primitive or a POD:

``````#include <iostream>

template <class T>
uint32_t hash(const T & key, uint32_t key_size, uint32_t table_size)
{
const uint8_t * bytes = reinterpret_cast<const uint8_t *>(&key);

uint32_t hash_value = 0;

for (uint32_t i = 0; i < key_size; i++)
{
hash_value += bytes[i] << (i % 16);
hash_value += bytes[i] << ((key_size - i) % 16);
}

return hash_value % table_size;
}

int main()
{
const uint32_t table_size = 19;

std::cout << hash("hello, world!", 13 * sizeof(char), table_size) << std::endl;
std::cout << hash("hello, again!", 13 * sizeof(char), table_size) << std::endl;

std::cout << hash(23, sizeof(int), table_size) << std::endl;
std::cout << hash(44, sizeof(int), table_size) << std::endl;
std::cout << hash(67, sizeof(int), table_size) << std::endl;

std::cout << hash(5.5, sizeof(double), table_size) << std::endl;
std::cout << hash(2.7, sizeof(double), table_size) << std::endl;
std::cout << hash(3.4, sizeof(double), table_size) << std::endl;

return 0;
}``````

A somewhat better hash function:

``````//...

for (uint32_t i = 0; i < key_size; i++)
{
hash_value += bytes[i] << ( 23 * i % 24 );
hash_value += bytes[i] << ( 23 * (key_size - i) % 24 );
}

//...``````

What's this thing with the 30 minute limit to editing?... -.-