template type from string at run time

S

shaun roe

I have a file which describes data and their type, unfortunately I have
to call a templatized function using the type I read in (these are
primitive types, by the way)

Is there any cleverer way other than doing a 'switch' statement on the
typename and calling the appropriate templatized function?

what I'm doing now is something like:
.... read in "myval" (which is a value) and its type as a strings...
if (inputType=="int"){
insert<int>(myval);
}
if (inputType=="unsigned long"){
insert<unsigned long>(myval);
}

if (inputType=="string"){
insert<string>(myval);
}


etc


cheers

shaun
 
V

Victor Bazarov

shaun said:
I have a file which describes data and their type, unfortunately I have
to call a templatized function using the type I read in (these are
primitive types, by the way)

Is there any cleverer way other than doing a 'switch' statement on the
typename and calling the appropriate templatized function?

what I'm doing now is something like:
... read in "myval" (which is a value) and its type as a strings...
if (inputType=="int"){
insert<int>(myval);
}
if (inputType=="unsigned long"){
insert<unsigned long>(myval);
}

if (inputType=="string"){
insert<string>(myval);
}


etc

"Cleverer" doesn't necessarily mean "better". You can create a map of
functions which insert, and then call that function:

map<string, insertionfunctionpointer> myinserters;

// fill 'myinserters' somehow:
myinserters["int"] = insert<int>;
myinserters["unsigned long"] = insert<unsigned long>;
...

// once you get the strings, this looks up the function and calls it:
myinserters[inputType](myval);

V
 
L

Luke Meyers

You should check out _C++ Templates: The Complete Guide_, by
Vandevoorde and Josuttis. They provide information on string template
parameters on pp. 40-41. The pattern they say works (haven't tried it
myself, yet) is:

template <char const* name>
class MyClass {
// ...
};

extern char const s[] = "hello";

MyClass<s> x;

Hope this helps. Buy the book, it's awesome.

Luke
 
J

JustBoo

Is there any cleverer way other than doing a 'switch' statement on the
typename and calling the appropriate templatized function?

I think the Standard is clear on 'cleverer' it's: more clever,
Cleaver. Which makes it clear, correct? Clyde concurs.

When I have understanding of computers I shall be the
Supreme Being! - Evil, Time Bandits
 
H

Howard

Luke Meyers said:
You should check out _C++ Templates: The Complete Guide_, by
Vandevoorde and Josuttis. They provide information on string template
parameters on pp. 40-41. The pattern they say works (haven't tried it
myself, yet) is:

template <char const* name>
class MyClass {
// ...
};

extern char const s[] = "hello";

MyClass<s> x;

Hope this helps. Buy the book, it's awesome.

Luke

That's a string parameter, not a string which defines the type of parameter.
The OP wanted, for example, the string "unsigned long" to result in passing
the <unsigned long> type specifier to the template. That's not something
the C++ language directly supports. Using 'if' statements of a map are
valid solutions.

-Howard
 
E

Earl Purple

Howard said:
Luke Meyers said:
You should check out _C++ Templates: The Complete Guide_, by
Vandevoorde and Josuttis. They provide information on string template
parameters on pp. 40-41. The pattern they say works (haven't tried it
myself, yet) is:

template <char const* name>
class MyClass {
// ...
};

extern char const s[] = "hello";

MyClass<s> x;

Hope this helps. Buy the book, it's awesome.

Luke

That's a string parameter, not a string which defines the type of parameter.
The OP wanted, for example, the string "unsigned long" to result in passing
the <unsigned long> type specifier to the template. That's not something
the C++ language directly supports. Using 'if' statements of a map are
valid solutions.

-Howard

It doesn't directly support code as text unlike Perl which does. The
totally generic solution would be to generate code, then compile it and
attempt to run it. Of course it would mean that either your own
software would have to compile and link, or you would have to have a
compiler on any machine that ran the program and get yours to invoke
it.

The advantage of using map is that it is extensible as long as wherever
you are extending it can get hold of a reference to the map.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top