hpp

S

stef

Hello,

Do you think it's a good idea writing code directly in an .hpp file ?
I noticed the famous "boost" library is doing that and... doesn't
seems to be "C++ coding standard".

thanks...
 
E

Erik Wikström

Hello,

Do you think it's a good idea writing code directly in an .hpp file ?
I noticed the famous "boost" library is doing that and... doesn't
seems to be "C++ coding standard".

When using templates you usually have to, since the definition of the
template needs to be included in every translation-unit that uses it. An
alternative approach (that amounts to the same thing) is to use have the
declarations in a .h-file and the implementation in a .hpp-file, and
then include the .hpp-file at the bottom of the .h-file.
 
R

Rahul

When using templates you usually have to, since the definition of the
template needs to be included in every translation-unit that uses it. An
alternative approach (that amounts to the same thing) is to use have the
declarations in a .h-file and the implementation in a .hpp-file, and
then include the .hpp-file at the bottom of the .h-file.

i think templates can be exported as a workaround... i have posted
regarding the same...
 
J

jkherciueh

Rahul said:
i think templates can be exported as a workaround... i have posted
regarding the same...

a) Exporting templates is not a workaround, rather it in the language for
exactly this purpose.

b) As of today, only very few compilers support export.


Best

Kai-Uwe Bux
 
R

Rahul

a) Exporting templates is not a workaround, rather it in the language for
exactly this purpose.

b) As of today, only very few compilers support export.

Best

Kai-Uwe Bux

Assume i have the following files,

temp.h

export template<typename T>
void foo(T object);

temp.cc

#include <iostream>

template <typename T>
void foo(T object)
{
cout<<"sample template function..."<<endl;
}

main.cc

#include <temp.h>

int main()
{
foo<int>(5);
return(0);
}

Now when i compile main.cc

gcc -c main.cc

how does the compiler know which file to refer to for the template
function definition? am i missing something?

Thanks in advance!!!
 
E

Erik Wikström

Assume i have the following files,

temp.h

export template<typename T>
void foo(T object);

temp.cc

#include <iostream>

template <typename T>
void foo(T object)
{
cout<<"sample template function..."<<endl;
}

main.cc

#include <temp.h>

int main()
{
foo<int>(5);
return(0);
}

Now when i compile main.cc

gcc -c main.cc

how does the compiler know which file to refer to for the template
function definition? am i missing something?

I am quite sure that gcc does not support export. Anyway, I suppose it
works just like when you do

foo.h:

class foo {
void bar()
};

foo.cpp:

void foo::bar() { /* ... */ }

main.cpp:

#include "foo.h"

int main() {
foo f;
f.bar();
}

In the above it is the linker who looks for the implementation, but I
suppose that a compiler can do it as well when looking for definitions
of templates.
 
R

Rahul

I am quite sure that gcc does not support export. Anyway, I suppose it
works just like when you do

foo.h:

class foo {
void bar()

};

foo.cpp:

void foo::bar() { /* ... */ }

main.cpp:

#include "foo.h"

int main() {
foo f;
f.bar();

}

In the above it is the linker who looks for the implementation, but I
suppose that a compiler can do it as well when looking for definitions
of templates.

yeah, it has to be the compiler as templates is all about source code
reusability...
 
R

Rolf Magnus

Rahul said:
Assume i have the following files,

temp.h

export template<typename T>
void foo(T object);

temp.cc

#include <iostream>

template <typename T>
void foo(T object)
{
cout<<"sample template function..."<<endl;
}

main.cc

#include <temp.h>

Should be:

#include "temp.h"
int main()
{
foo<int>(5);
return(0);
}

Now when i compile main.cc

gcc -c main.cc

how does the compiler know which file to refer to for the template
function definition? am i missing something?

You are missing that GCC doesn't support it at all. The above compiler
invocation prints:

../temp.h:1: warning: keyword 'export' not implemented, and will be ignored
 
J

Jerry Coffin

Assume i have the following files,

[ code elided ]
Now when i compile main.cc

gcc -c main.cc

how does the compiler know which file to refer to for the template
function definition? am i missing something?

Generally speaking, the compiler doesn't know where to find much of
anything on its own. The compiler deals exclusively with one file at a
time.

The linker is normally responsible for putting all the object files
together into an executable. When the compiler produces the object
files, they have external references -- i.e. references to things that
weren't defined in that file. The linker tries to find definitions in
the other files to satisfy those external references.

In the case of a template, the linker may find that the correct template
exists, but has not been instantiated over the correct type. Assuming it
supports export, the linker will then do something like re-invoking the
compiler, directing it to instantiate the template over a specified
type. The compiler does that, then the linker continues, with at least
that external reference satisfied.
 
J

James Kanze

Generally speaking, the compiler doesn't know where to find
much of anything on its own. The compiler deals exclusively
with one file at a time.
The linker is normally responsible for putting all the object
files together into an executable. When the compiler produces
the object files, they have external references -- i.e.
references to things that weren't defined in that file. The
linker tries to find definitions in the other files to satisfy
those external references.
In the case of a template, the linker may find that the
correct template exists, but has not been instantiated over
the correct type. Assuming it supports export, the linker will
then do something like re-invoking the compiler, directing it
to instantiate the template over a specified type. The
compiler does that, then the linker continues, with at least
that external reference satisfied.

That's one solution, but a compiler could also use some sort of
"implicit include"---CFront more or less did, although it did so
at link time, reinvoking the compiler with special options, and
Sun CC supported it for a long time (and maybe still does). All
it takes is a naming convention, so the compiler can find the
corresponding source file.

From the point of view of the standard, the difference with
export is that the implementation file is compiled in its own
separate context---nothing you can put in the client source can
affect names not looked up using dependent look-up. The larger
the project, the more important this becomes.

And although the standard doesn't say anything about this (the
standard doesn't even require separate compilation), the one
existing implementation will only recompile one file which uses
the template if the implementation is modified, rather than all,
as is the case otherwise. This can make a significant
difference in compile times during the development cycle if the
template is not yet stable---make a small change in one function
in the template to correct an error, and you only recompile one
of files in your test suite, rather than the entire test suite.
 

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

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,528
Latest member
AnaHawley8

Latest Threads

Top