Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compile time vs runtime?

Reply
Thread Tools

Compile time vs runtime?

 
 
desktop
Guest
Posts: n/a
 
      05-09-2007
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?
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      05-09-2007
On 9 Maj, 11:28, desktop <(E-Mail Removed)> 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.


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

 
Reply With Quote
 
 
 
 
desktop
Guest
Posts: n/a
 
      05-09-2007
Erik Wikström wrote:
> On 9 Maj, 11:28, desktop <(E-Mail Removed)> 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.

>
> 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?
 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      05-09-2007
On 9 Maj, 13:57, desktop <(E-Mail Removed)> wrote:
> Erik Wikström wrote:
> > On 9 Maj, 11:28, desktop <(E-Mail Removed)> 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.

>
> > 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

 
Reply With Quote
 
desktop
Guest
Posts: n/a
 
      05-09-2007
Erik Wikström wrote:
> On 9 Maj, 13:57, desktop <(E-Mail Removed)> wrote:
>> Erik Wikström wrote:
>>> On 9 Maj, 11:28, desktop <(E-Mail Removed)> 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.
>>> 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 23.

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?
 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      05-09-2007
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


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      05-09-2007
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.
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      05-09-2007
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.
 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      05-09-2007
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
 
Reply With Quote
 
desktop
Guest
Posts: n/a
 
      05-09-2007
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<>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
computation at compile time i.e. compile time functions usingtemplates Carter C++ 2 03-04-2009 06:43 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Re: compile time efficency / run time flexibility - best of both worlds Daniel T. C++ 2 10-19-2004 02:02 AM
run-time vs compile-time newbiecpp C++ 17 09-11-2004 02:10 PM



Advertisments