473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

template type only known at runtime

Hi,

I want to do able to declare an object bar of a templated class foo
where the actual template type of foo is only know at runtime:

-----

#include <string>

template <class Tclass foo {
T x;
public:
void do_something_depending_on_type_of_T(){

...
};
}

int main(int argc, char *argv[]) {
if (argc==1) //default type string should be used
foo<std::stringbar;
else //use class int
foo<intbar;
bar.do_something_depending_on_type_of_T();
return 0;
}

-----

I know it doesn't work like this but is it possible at all?

Ralf

Nov 18 '08 #1
8 6766
On 18 nov, 08:51, Ralf Goertz
<r_goe...@expires-2006-11-30.arcornews.dewrote:
Hi,

I want to do able to declare an object bar of a templated class foo
where the actual template type of foo is only know at runtime:

-----

#include <string>

template <class Tclass foo {
* * T x;
public:
* * void do_something_depending_on_type_of_T(){

* * * * ...
* * };

}

int main(int argc, char *argv[]) {
* * if (argc==1) //default type string should be used
* * * * foo<std::stringbar;
* * else //use class int
* * * * foo<intbar;
* * bar.do_something_depending_on_type_of_T();
* * return 0;

}
Template based code is constructed at compile time (static
polymorphism). If you need dynamic polymorphism use virtual functions.
However, your code has a general C++ problem (not specific to
templates). The line bar.do_something_depending_on_type_of_T(); will
generate an error because bar is undefined. This would happen even if
bar was not a template.

I don't know if this is the best design for you. But the closest I can
get to it is through template specialization:

emplate <class Tclass foo
{
public:
void do_something()
{} //You don't need trailing semi-colon here.
}; //You need it here though.

template <class foo<std::string>
{
public:
void do_something()
{ /*Implementation for string */ }
};

template <class foo<int>
{
public:
void do_something()
{ /*Implementation for int */ }
};

int main(int argc, char *argv[])
{
foo<std::stringstringBar;
foo<intintBar;
if (argc==1)
stringBar.do_something();
else
intBar.do_something();
return 0;
}

--
Leandro T. C. Melo
Nov 18 '08 #2
On Nov 18, 1:51*pm, Ralf Goertz
<r_goe...@expires-2006-11-30.arcornews.dewrote:
I know it doesn't work like this but is it possible at all?
It wouldn't work even if you solve your problems with templates
because of scoping:
int main(int argc, char *argv[]) {
if (argc==1) //default type string should be used
foo<std::stringbar;
else //use class int
foo<intbar;
bar.do_something_depending_on_type_of_T();
bar is undefined here (out of scope).
return 0;
}
Nov 18 '08 #3
On Nov 18, 1:51*pm, Ralf Goertz
<r_goe...@expires-2006-11-30.arcornews.dewrote:
Hi,

I want to do able to declare an object bar of a templated class foo
where the actual template type of foo is only know at runtime:
The most painless solution I think looks like:

int main(int argc, char *argv[]) {
* * if (argc == 1) {
* * * * foo<std::stringbar;
bar.do_something_depending_on_type_of_T();
} else {
* * * * foo<intbar;
bar.do_something_depending_on_type_of_T();
}
* * return 0;
}

Of course, may be a better solution also exists.
Nov 18 '08 #4
maverik wrote:
On Nov 18, 1:51*pm, Ralf Goertz
<r_goe...@expires-2006-11-30.arcornews.dewrote:
>I know it doesn't work like this but is it possible at all?

It wouldn't work even if you solve your problems with templates
because of scoping:
I know about the scoping problem. The code was just there to illustrate
what I want which was hard for me to describe in prose. The problem is
similar to one I had a year ago or so. I had wanted to create a
reference to cin or an ifstream object depending on how the program was
invoked. At that time I was wondering why C++ didn't have a deferred
intialization feature for references like

istream &config;
ifstream ifs;
if (config_is_read_from_cin) config=cin; else config=ifs;

It can be done with pointers why not with references. Anyway, it seems I
have to use the apprach you gave in the other posting, thanks, also to
Leandro.

Ralf
Nov 18 '08 #5
Ralf Goertz wrote:
maverik wrote:
>On Nov 18, 1:51 pm, Ralf Goertz
<r_goe...@expires-2006-11-30.arcornews.dewrote:
>>I know it doesn't work like this but is it possible at all?
It wouldn't work even if you solve your problems with templates
because of scoping:

I know about the scoping problem. The code was just there to illustrate
what I want which was hard for me to describe in prose. The problem is
similar to one I had a year ago or so. I had wanted to create a
reference to cin or an ifstream object depending on how the program was
invoked. At that time I was wondering why C++ didn't have a deferred
intialization feature for references like

istream &config;
ifstream ifs;
if (config_is_read_from_cin) config=cin; else config=ifs;

It can be done with pointers why not with references. [...]
std::istream& is = config_is_read_from_cin ? std::cin : ifs;
Ralf
Schobi
Nov 18 '08 #6
maverik wrote:
On Nov 18, 1:51 pm, Ralf Goertz
<r_goe...@expires-2006-11-30.arcornews.dewrote:
>Hi,

I want to do able to declare an object bar of a templated class foo
where the actual template type of foo is only know at runtime:

The most painless solution I think looks like:

int main(int argc, char *argv[]) {
if (argc == 1) {
foo<std::stringbar;
bar.do_something_depending_on_type_of_T();
} else {
foo<intbar;
bar.do_something_depending_on_type_of_T();
}
return 0;
}

Of course, may be a better solution also exists.
template< class T >
inline void do_it() {foo<int>().do_something_depending_on_type_of_T(); )

int main(int argc, char *argv[]) {
if (argc == 1) {
do_it<std::string>();
} else {
do_it<int>();
}
return 0;
}

Schobi
Nov 18 '08 #7
Ralf Goertz wrote:
Hi,

I want to do able to declare an object bar of a templated class foo
where the actual template type of foo is only know at runtime:
If the type is only known at run-time, you need run-time
polymorphy, which is done using virtual functions and
inheritance. However, that doesn't mean you can't use
templates at all:

class foo_base {
public:
virtual void do_something_depending_on_type_of_T() = 0;
};

template< typename T >
class foo : public foo_base {
public:
virtual void do_something_depending_on_type_of_T() {}
};
[...]
Ralf
Schobi
Nov 18 '08 #8
Hendrik Schober wrote:
Ralf Goertz wrote:
>Hi,

I want to do able to declare an object bar of a templated class foo
where the actual template type of foo is only know at runtime:

If the type is only known at run-time, you need run-time
polymorphy, which is done using virtual functions and
inheritance. However, that doesn't mean you can't use
templates at all:

class foo_base {
public: virtual void do_something_depending_on_type_of_T() = 0; };
virtual void do_something_depending_on_type_of_T() = 0;
};

template< typename T >
class foo : public foo_base {
public:
virtual void do_something_depending_on_type_of_T() {}
};

Actually, I just need two different types for the template,
foo<std::stringand foo<int>. The only difference is the id type I get
from a database, it can be either integer or char and is known only at
runtime. If it is numeric I still have to do some calculations with it
(so I can't just sql-cast it to char and use std::string in my program).
Also, the way of writing back the data to the db differs because of
quoting. "id" must be part of the class as I also need it for
std::map<T,doublewithin class foo. But wrapping it in another template
function as you also suggested seems to be the way to go.

Thanks for the ternary ?: suggestion in the other posting!

Ralf
Nov 19 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
4
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
5
by: chrisstankevitz | last post by:
Hi, Q1: Is there a way to make a template function that works only for specific types which produces a compiler error if used with an invalid type? Q2: If not, how do people deal with this...
4
by: David Sanders | last post by:
Hi, I have a class depending on a template parameter: template<int n> class MyClass { }; I want the template parameter to be chosen according to a variable, e.g.
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.