can you template a single variable?

i would like to have a non global, template variable to use for input in 'main' only

i have a struct for this but if there is a better way i would rather use it.
I'm not sure I understand what you're asking.

What are you trying to do?

EDIT:

FWIW, you can't template an individual variable -- and it doesn't make any sense to do that anyway.
Last edited on
never mind, i think i got confused on how the templates work. does the type always have to be hard coded or can it be user specified?
The compiler needs to know the type, so it has to be hardcoded.

For types that can change at runtime (user specified), there are options. boost::any is one.
boost::any?
an enum and a union inside a struct:
1
2
3
4
5
6
7
8
struct anytype {
    enum {typeint,typefloat,typecharp} type;
    union {
    int i;
    float f;
    char * s;
    } val;
}
rocketboy's suggestion will also work, but only for basic types (something like a string wouldn't work)

boost::any is part of the boost library - it's something you'd have to download and install.

see www.boost.org

I'm not all that familiar with boost, honestly.
For rocketboy's suggestion:

what is the enum used for?
It's to keep track of the variable type.
Topic archived. No new replies allowed.