Skip to main content
gamedev.net
🔒 Locked

Calling template function without arguments

Started by jden Sep 23, 2010 at 2:52 PM 7 replies 3.2k views
Original Post
jden
jden
Hi,
My question is about making a call with no arguments to a template function.
I have not seen anything else than the type-object declaration, like
class name;
What I would like to do is to use a template function in which I can input
something, a generic value with other words. I try to calling it from main. But
the program shouldn't know the type before me. Maybe making a typeless object creation
could do it, but I can't figure it out how to do it?
SiCrane
SiCrane
If I understand what you're asking you can do it like this:
template <typename T>void foo(void) {  std::cout << typeid(T).name() << std::endl;}int main(int, char **) {  foo<int>();  foo<float>();  return 0;}
jden
jden
Well, in this case I want just cin >>, inside the function. cout works because the type
is already known.


int main()
{
C cObj; // Want a generic type ( not only ,..)
cObj.input(); // Calls input()


return 0;
}

h-file:

template
class C
{

public:

void input() // Any way to make it return (generic also)?
{

cin >> x; // Any type input

}


private:

T x;


};
SiCrane
SiCrane
I'm still not clear on what you're trying to accomplish.
SiS-Shadowman
SiS-Shadowman
I think he wants to create a certain object from a stream without knowing what type is stored in the stream.
Aka creating an int because cin reads a number.

If this is what you want, then you will need to write some code that detects what type is actually entered. There's nothing in the standard (afaik) that would do such a thing: In the end, when someone entered "5", do you want to store it in a char, short, int or long int? Is it signed or unsigned?

You will need to be more specific about your problem before we can offer any solution.
jden
jden
Yes. Create an object, call the function, enter input, return and save. All done
with a class template.

The whole idea is to make the code shorter with just one function.

The only half solution I can find is that main needs to know what type the user is going to input.
The template doesn't need to know anything. It just saves whatever is entered and should return the type. But how can it return a generic type?

SiCrane
SiCrane
Templates are used when you know at compile time what types you need. What you want is runtime polymorphism. The particular problem you're looking sounds like either lexing or parsing.
SiS-Shadowman
SiS-Shadowman
You cannot solve that problem with templates. Template types are resolved at compile time: the compiler must know which type is actually used.

What you want is no easy task with c++: You will have to write a lot of code yourself. I gave you some hints in my post earlier: Basically retrieve the string from std::cin and then determine yourself which type could be retrieved from it.
jden
jden
Ok thanks, I think your answers is good enough for now.
The closest way of doing it (what I know). At least know
the type in main, determine a receiver value, and make a generic function like:
//typename T
T input(){ cin >> value; return value;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.