
January 11th, 2006, 09:55 PM
| | | template type from string at run time
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 | 
January 11th, 2006, 10:05 PM
| | | Re: template type from string at run time
shaun roe wrote:[color=blue]
> 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[/color]
"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 | 
January 11th, 2006, 10:45 PM
| | | Re: template type from string at run time
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 | 
January 11th, 2006, 10:45 PM
| | | Re: template type from string at run time
On Wed, 11 Jan 2006 22:42:43 +0100, shaun roe <shaun.roe@wanadoo.fr>
wrote:
[color=blue]
>Is there any cleverer way other than doing a 'switch' statement on the
>typename and calling the appropriate templatized function?[/color]
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 | 
January 12th, 2006, 05:55 PM
| | | Re: template type from string at run time
"Luke Meyers" <n.luke.meyers@gmail.com> wrote in message
news:1137018828.612742.50510@g49g2000cwa.googlegro ups.com...[color=blue]
> 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
>[/color]
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 | 
January 12th, 2006, 07:05 PM
| | | Re: template type from string at run time
Howard wrote:[color=blue]
> "Luke Meyers" <n.luke.meyers@gmail.com> wrote in message
> news:1137018828.612742.50510@g49g2000cwa.googlegro ups.com...[color=green]
> > 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
> >[/color]
>
> 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[/color]
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. | 
January 12th, 2006, 08:35 PM
| | | Re: template type from string at run time
"Howard" <alicebt@hotmail.com> wrote in message
news:hvwxf.250392$qk4.12926@bgtnsc05-news.ops.worldnet.att.net...
[color=blue]
> ... Using 'if' statements of a map are valid solutions.[/color]
(should read "OR a map", not "OF a map") |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|