473,474 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Compile time vs runtime?

I have read that using templates makes types know at compile time and
using inheritance the types are first decided at runtime. The use of
pointers and casts also indicates that the types will first be know at
runtime.

But is there some strict definitions that defines runtime code and
compile time code that can be used in general?
May 9 '07 #1
16 5396
On 9 Maj, 11:28, desktop <f...@sss.comwrote:
I have read that using templates makes types know at compile time and
using inheritance the types are first decided at runtime. The use of
pointers and casts also indicates that the types will first be know at
runtime.
Don't know if this answers your question or not; the idea behind
templates it that you at compile-time knows all the types that can be
used, and then generate code for each of those types. That's why it's
called templates, since it's not the code you write that gets executed
since it's just a template used by the compiler to generate the code
that will be executed.
But is there some strict definitions that defines runtime code and
compile time code that can be used in general?
Compile-time is compile-time, meaning that all the magic takes place
before the application is compiled, which is about as typesafe as you
can get. With runtime the magic happens when needed and have not been
fully checked at compilation. This means that for compile-time
polymorphism you need to know most if not everything about the types
at compile-time, which is why you can't have containers that can be
used with any type as compiled code, but have to distribute the source
(see the standard containers).

--
Erik Wikström

May 9 '07 #2
Erik Wikström wrote:
On 9 Maj, 11:28, desktop <f...@sss.comwrote:
>I have read that using templates makes types know at compile time and
using inheritance the types are first decided at runtime. The use of
pointers and casts also indicates that the types will first be know at
runtime.

Don't know if this answers your question or not; the idea behind
templates it that you at compile-time knows all the types that can be
used, and then generate code for each of those types. That's why it's
called templates, since it's not the code you write that gets executed
since it's just a template used by the compiler to generate the code
that will be executed.
>But is there some strict definitions that defines runtime code and
compile time code that can be used in general?

Compile-time is compile-time, meaning that all the magic takes place
before the application is compiled, which is about as typesafe as you
can get. With runtime the magic happens when needed and have not been
fully checked at compilation. This means that for compile-time
polymorphism you need to know most if not everything about the types
at compile-time, which is why you can't have containers that can be
used with any type as compiled code, but have to distribute the source
(see the standard containers).

Is not possible to have a heterogeneous container at compile-time? I
have read about a method that uses the heap and makes sure that each
element in the container is allocated:

http://gethelp.devx.com/techtips/cpp.../10min0900.asp

Another technique deals with simulating dynamic polymorphism (deriving
classes from a base class) with the "The Curiously Recurring Template
Pattern": CRTP:

http://en.wikipedia.org/wiki/Curious...mplate_Pattern

Would either of these method not help me to generate a container with
different types at compile-time?
May 9 '07 #3
On 9 Maj, 13:57, desktop <f...@sss.comwrote:
Erik Wikström wrote:
On 9 Maj, 11:28, desktop <f...@sss.comwrote:
I have read that using templates makes types know at compile time and
using inheritance the types are first decided at runtime. The use of
pointers and casts also indicates that the types will first be know at
runtime.
Don't know if this answers your question or not; the idea behind
templates it that you at compile-time knows all the types that can be
used, and then generate code for each of those types. That's why it's
called templates, since it's not the code you write that gets executed
since it's just a template used by the compiler to generate the code
that will be executed.
But is there some strict definitions that defines runtime code and
compile time code that can be used in general?
Compile-time is compile-time, meaning that all the magic takes place
before the application is compiled, which is about as typesafe as you
can get. With runtime the magic happens when needed and have not been
fully checked at compilation. This means that for compile-time
polymorphism you need to know most if not everything about the types
at compile-time, which is why you can't have containers that can be
used with any type as compiled code, but have to distribute the source
(see the standard containers).

Is not possible to have a heterogeneous container at compile-time? I
have read about a method that uses the heap and makes sure that each
element in the container is allocated:

http://gethelp.devx.com/techtips/cpp.../10min0900.asp
In this article they store pointers to a base-class, so the elements
stored are all of the same type. However when you dereference them you
can utilize runtime polymorphism.
Another technique deals with simulating dynamic polymorphism (deriving
classes from a base class) with the "The Curiously Recurring Template
Pattern": CRTP:

http://en.wikipedia.org/wiki/Curious...mplate_Pattern

Would either of these method not help me to generate a container with
different types at compile-time?
No, read this entry in the FAQ for a description about how to create
heterogeneous container using templates, notice that the first method
is the same as in the first article you posted.
http://www.parashift.com/c++-faq-lit....html#faq-34.4

--
Erik Wikström

May 9 '07 #4
Erik Wikström wrote:
On 9 Maj, 13:57, desktop <f...@sss.comwrote:
>Erik Wikström wrote:
>>On 9 Maj, 11:28, desktop <f...@sss.comwrote:
I have read that using templates makes types know at compile time and
using inheritance the types are first decided at runtime. The use of
pointers and casts also indicates that the types will first be know at
runtime.
Don't know if this answers your question or not; the idea behind
templates it that you at compile-time knows all the types that can be
used, and then generate code for each of those types. That's why it's
called templates, since it's not the code you write that gets executed
since it's just a template used by the compiler to generate the code
that will be executed.
But is there some strict definitions that defines runtime code and
compile time code that can be used in general?
Compile-time is compile-time, meaning that all the magic takes place
before the application is compiled, which is about as typesafe as you
can get. With runtime the magic happens when needed and have not been
fully checked at compilation. This means that for compile-time
polymorphism you need to know most if not everything about the types
at compile-time, which is why you can't have containers that can be
used with any type as compiled code, but have to distribute the source
(see the standard containers).
Is not possible to have a heterogeneous container at compile-time? I
have read about a method that uses the heap and makes sure that each
element in the container is allocated:

http://gethelp.devx.com/techtips/cpp.../10min0900.asp

In this article they store pointers to a base-class, so the elements
stored are all of the same type. However when you dereference them you
can utilize runtime polymorphism.
>Another technique deals with simulating dynamic polymorphism (deriving
classes from a base class) with the "The Curiously Recurring Template
Pattern": CRTP:

http://en.wikipedia.org/wiki/Curious...mplate_Pattern

Would either of these method not help me to generate a container with
different types at compile-time?

No, read this entry in the FAQ for a description about how to create
heterogeneous container using templates, notice that the first method
is the same as in the first article you posted.
http://www.parashift.com/c++-faq-lit....html#faq-34.4

--
Erik Wikström
Thanks for the link I will look into the methods described.

I thought that with dynamic polymorphism heterogeneous containers was
not an issue, at least that is what my book says (C++ Templates: The
complete Guide page 238).

Heterogeneous containers first becomes a problem when dealing with
static polymorphism. But as it says on the wiki page in regard to CRTP:

"This technique achieves a similar effect to the use of virtual
functions, without the costs (and some flexibility) of dynamic polymorphism"
So I still can't see why CRTP does not provide at pattern for
heterogeneous containers with static polymorphism.

With CRTP you can create a base class "Base" and then a number of
derived classes and get the same functionality as with virtual functions
but without the runtime cost. So making a container with this base
class should in theory give me a heterogeneous container.

If CRTP cannot be used to make a heterogeneous container what kind of
purpose does it have?
May 9 '07 #5
desktop wrote:
Would either of these method not help me to generate a container with
different types at compile-time?
Is not possible to generate a container with different types at
compile-time. Everything is at compile-time is static, frozen.
Obviously, a container is not frozen, that is, you don't know all the
types of all the elements of an heterogeneus container before actually
launching the program.

Regards,

Zeppe
May 9 '07 #6
desktop wrote:
I have read that using templates makes types know at compile time and
using inheritance the types are first decided at runtime. The use of
pointers and casts also indicates that the types will first be know at
runtime.

But is there some strict definitions that defines runtime code and
compile time code that can be used in general?
One good rule that is quite important is that name and type resolution
is "static" (compile time). I think you can pretty much use this rule
to derive the rest.

The only remotely exception to this rule is RTTI, which is not complete
enough to truly resolve a type. You can use dynamic casting to be sure
you are casting to a compatible type but you can't ever find the real
type of your object.

Everything else is done through static resolution through the variable
that is being manipulated followed by some pointer dereferencing.
May 9 '07 #7
Zeppe wrote:
desktop wrote:
>Would either of these method not help me to generate a container with
different types at compile-time?

Is not possible to generate a container with different types at
compile-time. Everything is at compile-time is static, frozen.
Obviously, a container is not frozen, that is, you don't know all the
types of all the elements of an heterogeneus container before actually
launching the program.
True, but you can make a container that contains objects that
heterogeneously wrap any other object.
May 9 '07 #8
Noah Roberts wrote:
Zeppe wrote:
>desktop wrote:
>>Would either of these method not help me to generate a container with
different types at compile-time?

Is not possible to generate a container with different types at
compile-time. Everything is at compile-time is static, frozen.
Obviously, a container is not frozen, that is, you don't know all the
types of all the elements of an heterogeneus container before actually
launching the program.

True, but you can make a container that contains objects that
heterogeneously wrap any other object.
Yes, but the type of the wrapped object is to be determined at run-time.

Regards,

Zeppe
May 9 '07 #9
Zeppe wrote:
Noah Roberts wrote:
>Zeppe wrote:
>>desktop wrote:

Would either of these method not help me to generate a container
with different types at compile-time?

Is not possible to generate a container with different types at
compile-time. Everything is at compile-time is static, frozen.
Obviously, a container is not frozen, that is, you don't know all the
types of all the elements of an heterogeneus container before
actually launching the program.

True, but you can make a container that contains objects that
heterogeneously wrap any other object.

Yes, but the type of the wrapped object is to be determined at run-time.

Regards,

Zeppe

Just to be sure: If one writes runtime code it is possible to make a
heterogeneous container. As an example I have the following:
class Base {
....
};

class BaseOne : public Base {
....
};

class BaseTwo : public Base {
....
};
I can now make:

std::vector<Base*bases;

Since I am using a pointer to a Base object I can still add BaseOne and
BaseTwo to the container since since they have Base as their base class.

At runtime it will be decided which types are in the container.

So as long as you write runtime code its possible to make a
heterogeneous container.

Its only when one wants compile time code that its impossible to have a
heterogeneous container.

the type of the object will be

If I have a few classes that inherits from Vector I can make:

std::vector<>
May 9 '07 #10
desktop wrote:
Zeppe wrote:
>Noah Roberts wrote:
>>Zeppe wrote:
desktop wrote:

Would either of these method not help me to generate a container
with different types at compile-time?

Is not possible to generate a container with different types at
compile-time. Everything is at compile-time is static, frozen.
Obviously, a container is not frozen, that is, you don't know all
the types of all the elements of an heterogeneus container before
actually launching the program.

True, but you can make a container that contains objects that
heterogeneously wrap any other object.

Yes, but the type of the wrapped object is to be determined at run-time.

Regards,

Zeppe


Just to be sure: If one writes runtime code it is possible to make a
heterogeneous container. As an example I have the following:
class Base {
...
};

class BaseOne : public Base {
...
};

class BaseTwo : public Base {
...
};
I can now make:

std::vector<Base*bases;

Since I am using a pointer to a Base object I can still add BaseOne and
BaseTwo to the container since since they have Base as their base class.

At runtime it will be decided which types are in the container.
Nope. What type is in the container is decided at compile time. That
type is pointer to Base.
May 9 '07 #11
Noah Roberts wrote:
desktop wrote:
>Zeppe wrote:
>>Noah Roberts wrote:
Zeppe wrote:
desktop wrote:
>
>Would either of these method not help me to generate a container
>with different types at compile-time?
>
Is not possible to generate a container with different types at
compile-time. Everything is at compile-time is static, frozen.
Obviously, a container is not frozen, that is, you don't know all
the types of all the elements of an heterogeneus container before
actually launching the program.

True, but you can make a container that contains objects that
heterogeneously wrap any other object.

Yes, but the type of the wrapped object is to be determined at run-time.

Regards,

Zeppe


Just to be sure: If one writes runtime code it is possible to make a
heterogeneous container. As an example I have the following:
class Base {
...
};

class BaseOne : public Base {
...
};

class BaseTwo : public Base {
...
};
I can now make:

std::vector<Base*bases;

Since I am using a pointer to a Base object I can still add BaseOne
and BaseTwo to the container since since they have Base as their base
class.

At runtime it will be decided which types are in the container.

Nope. What type is in the container is decided at compile time. That
type is pointer to Base.
Ok but the container is still heterogeneous since it can both contain
BaseOne and BaseTwo pointers even though is created for type Base pointer.
May 9 '07 #12
On 2007-05-09 22:13, desktop wrote:
Noah Roberts wrote:
>desktop wrote:
>>Zeppe wrote:
Noah Roberts wrote:
Zeppe wrote:
>desktop wrote:
>>
>>Would either of these method not help me to generate a container
>>with different types at compile-time?
>>
>Is not possible to generate a container with different types at
>compile-time. Everything is at compile-time is static, frozen.
>Obviously, a container is not frozen, that is, you don't know all
>the types of all the elements of an heterogeneus container before
>actually launching the program.
>
True, but you can make a container that contains objects that
heterogeneously wrap any other object.

Yes, but the type of the wrapped object is to be determined at run-time.

Regards,

Zeppe
Just to be sure: If one writes runtime code it is possible to make a
heterogeneous container. As an example I have the following:
class Base {
...
};

class BaseOne : public Base {
...
};

class BaseTwo : public Base {
...
};
I can now make:

std::vector<Base*bases;

Since I am using a pointer to a Base object I can still add BaseOne
and BaseTwo to the container since since they have Base as their base
class.

At runtime it will be decided which types are in the container.

Nope. What type is in the container is decided at compile time. That
type is pointer to Base.

Ok but the container is still heterogeneous since it can both contain
BaseOne and BaseTwo pointers even though is created for type Base pointer.
Not really, a true heterogeneous container can contain objects of
different types, in this case there is only one type and that is a
pointer to Base. What makes this work close enough as a heterogeneous
container is the fact that the pointer to Base does not have to point to
an object of type Base, it might just as well point to an object of a
type derived from Base (BaseOne or BaseTwo).

If the type Base have any virtual methods and you call any of them
through dereferencing a pointer to Base (or a reference) then dynamic
binding comes into play and makes sure that the correct method in the
derived class that the pointer actually points to is called.

However if you in one of the derived types adds a method (that did not
exist in Base) and try to call it using a pointer to Base it will not
work since Base does not have that method.

--
Erik Wikström
May 9 '07 #13
desktop wrote:
I have read that using templates makes types know at compile time and
using inheritance the types are first decided at runtime. The use of
pointers and casts also indicates that the types will first be know at
runtime.
I think you have a slight confusion here.

Inheritance per se does not generate runtime type resolving code.
Only if the classes have virtual functions and they are called the
functions which actually get called is resolved at runtime.
Non-virtual functions can be resolved at compile-time (well, at
linking stage, more precisely).

Pointers and casts in general do not generate runtime code. The
only cast which does is a dynamic upcast. (There might also be some
multiple inheritance cases where downcasting actually changes the
value of the pointer, but by how much this is changed can, afaik,
be resolved at compile time.)
May 10 '07 #14
Juha Nieminen wrote:
desktop wrote:
>I have read that using templates makes types know at compile time and
using inheritance the types are first decided at runtime. The use of
pointers and casts also indicates that the types will first be know at
runtime.

I think you have a slight confusion here.

Inheritance per se does not generate runtime type resolving code.
Only if the classes have virtual functions and they are called the
functions which actually get called is resolved at runtime.
Non-virtual functions can be resolved at compile-time (well, at
linking stage, more precisely).

Pointers and casts in general do not generate runtime code. The
only cast which does is a dynamic upcast. (There might also be some
multiple inheritance cases where downcasting actually changes the
value of the pointer, but by how much this is changed can, afaik,
be resolved at compile time.)
On this page:

http://en.wikipedia.org/wiki/Curious...mplate_Pattern

and alternative to paying the price for virtual functions at runtime is
the pattern CRTP. Would this pattern not give my the same functionality
but just at compile time?
May 10 '07 #15
desktop wrote:
On this page:

http://en.wikipedia.org/wiki/Curious...mplate_Pattern

and alternative to paying the price for virtual functions at runtime is
the pattern CRTP. Would this pattern not give my the same functionality
but just at compile time?
No. With the CRTP, you don't have a common base class where you can call
member functions:

template <typename Derived>
class base<Derived{};

class derived1 : public base<derived1{};
class derived2 : public base<derived2{};

base<derived1and base<derived2are different types, so you can't put
them in the same container and you can't have a pointer of base* that can
hold both types.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
May 10 '07 #16
Juha Nieminen wrote:
Pointers and casts in general do not generate runtime code.
The only cast which does is a dynamic upcast. (There might
also be some multiple inheritance cases where downcasting
actually changes the value of the pointer,
You don't need multiple-inheritance to screw things up:

#include <iostream>
class a{char c;};
class b: public a{virtual void whatever(){};};
int main(int argc, char* argv[]) {
b x;
std::cout << &x << " " << (a*)&x << "\n";
return 0;}
but by how much this is changed can, afaik,
be resolved at compile time.)
Except when downcasting to a virtual base class:

#include <iostream>
class a{char c;};
class b: public virtual a {};
class c1: public b {};
class c2: public b {};
class d: public c1, public c2 {};
int main(int argc, char* argv[]) {
d x;
b *b1 = (c1*)&x;
b *b2 = (c2*)&x;
std::cout << b1 << " " << (a*)b1 << "\n";
std::cout << b2 << " " << (a*)b2 << "\n";
return 0;}

Here, b1 and b2 point to different objects sharing the same a, so the
offsets differ and have be stored in the vtable.
May 17 '07 #17

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

Similar topics

19
by: Christopher | last post by:
I am getting a parse error from g++ pointing at my catch line and can't figure out whats wrong with this code: #include "BigPosInt.h" #include <iostream> #include <new> #include <assert.h> ...
7
by: Samee Zahur | last post by:
Hello, The other day I was rather shocked to find that I couldn't find a good use for runtime polymorphism! Let me explain this a bit further before you get shocked. Any function that I could...
3
by: Eric Newton | last post by:
I'm wondering if there's a compile time cast gaurantee if any? Given the following: //----- begin sample code public interface IRegion { string Text { get; } RectangleF Area { get; } }
2
by: Glen | last post by:
I'm working on a custom assembly and I'm trying to figure out the best approach to handling known constraints within the assembly, once compiled, to alert the developer at compile time of a...
4
by: Dave Rahardja | last post by:
I have the following program that uses an array of chars to simulate a bit set: --------- // An out-of-bounds exception class BoundsException {}; template <int bits = 1> class Bitset
4
by: John Smith | last post by:
Hi I'm porting some C++ code to new platforms and have some 1-byte aligned structures which need a specific size. Since datatypes can vary on different platforms (which I found out the hard way...
7
by: wei8010 | last post by:
int fun() { static int x = 1000; return 0; } int main( int argc, char* argv ) { cout<<x<<endl; }
9
by: ThunderMusic | last post by:
Hi, I'd like to create a compile time error in my class... maybe there's a way already built in in the framework so I can achieve what I want... I have 2 constructors in my class. One of them...
7
by: Rick | last post by:
With String.Format, if I have an incorrect number of args specified for a format string, compile fails. How can I implement similar design-time functionality for my own string functions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.