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...
.
|
|
| User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=" |
|
| Title: Re: hpp |
13 Jan 2008 06:22:34 AM |
|
|
On 2008-01-13 12:53, stef wrote:
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.
--
Erik Wikström
.
|
|
|
| User: "Rahul" |
|
| Title: Re: hpp |
13 Jan 2008 07:33:01 AM |
|
|
On Jan 13, 5:22 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
On 2008-01-13 12:53, stef wrote:
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.
--
Erik Wikstr=F6m
i think templates can be exported as a workaround... i have posted
regarding the same...
.
|
|
|
| User: "" |
|
| Title: Re: hpp |
13 Jan 2008 07:37:58 AM |
|
|
Rahul wrote:
On Jan 13, 5:22 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
On 2008-01-13 12:53, stef wrote:
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.
--
Erik Wikström
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
.
|
|
|
| User: "Rahul" |
|
| Title: Re: hpp |
13 Jan 2008 08:04:06 AM |
|
|
On Jan 13, 6:37 pm, wrote:
Rahul wrote:
On Jan 13, 5:22 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
On 2008-01-13 12:53, stef wrote:
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. A=
n
alternative approach (that amounts to the same thing) is to use have th=
e
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.
--
Erik Wikstr=F6m
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
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!!!
.
|
|
|
| User: "Jerry Coffin" |
|
| Title: Re: hpp |
13 Jan 2008 02:05:10 PM |
|
|
In article <965d1d23-c2e3-4bab-a91e-6233bfc14086
@l1g2000hsa.googlegroups.com>, says...
On Jan 13, 6:37 pm, wrote:
Rahul wrote:
On Jan 13, 5:22 pm, Erik Wikstr=C3=B6m <Erik-wikst...@telia.com> wrot=
e:
On 2008-01-13 12:53, stef wrote:
Hello,
Do you think it's a good idea writing code directly in an .hpp fil=
e ?
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 th=
e
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.
--
Erik Wikstr=C3=B6m
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 f=
or
exactly this purpose.
b) As of today, only very few compilers support export.
Best
Kai-Uwe Bux
=20
Assume i have the following files,
[ code elided ]=20
Now when i compile main.cc
=20
gcc -c main.cc
=20
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=20
anything on its own. The compiler deals exclusively with one file at a=20
time.
The linker is normally responsible for putting all the object files=20
together into an executable. When the compiler produces the object=20
files, they have external references -- i.e. references to things that=20
weren't defined in that file. The linker tries to find definitions in=20
the other files to satisfy those external references.
In the case of a template, the linker may find that the correct template=20
exists, but has not been instantiated over the correct type. Assuming it=20
supports export, the linker will then do something like re-invoking the=20
compiler, directing it to instantiate the template over a specified=20
type. The compiler does that, then the linker continues, with at least=20
that external reference satisfied.=20
--=20
Later,
Jerry.
The universe is a figment of its own imagination.
.
|
|
|
| User: "James Kanze" |
|
| Title: Re: hpp |
14 Jan 2008 04:38:12 AM |
|
|
On Jan 13, 9:05 pm, Jerry Coffin <jcof...@taeus.com> wrote:
[...]
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.
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.
=46rom 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.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
|
|
| User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=" |
|
| Title: Re: hpp |
13 Jan 2008 08:14:05 AM |
|
|
On 2008-01-13 15:04, Rahul wrote:
On Jan 13, 6:37 pm, wrote:
Rahul wrote:
On Jan 13, 5:22 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
On 2008-01-13 12:53, stef wrote:
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.
--
Erik Wikström
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
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.
--
Erik Wikström
.
|
|
|
| User: "Rahul" |
|
| Title: Re: hpp |
13 Jan 2008 08:24:48 AM |
|
|
On Jan 13, 7:14 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
On 2008-01-13 15:04, Rahul wrote:
On Jan 13, 6:37 pm, wrote:
Rahul wrote:
On Jan 13, 5:22 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
On 2008-01-13 12:53, stef wrote:
Hello,
Do you think it's a good idea writing code directly in an .hpp fil=
e ?
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 th=
e
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.
--
Erik Wikstr=F6m
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 f=
or
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?
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.
--
Erik Wikstr=F6m
yeah, it has to be the compiler as templates is all about source code
reusability...
.
|
|
|
| User: "James Kanze" |
|
| Title: Re: hpp |
13 Jan 2008 02:25:04 PM |
|
|
On Jan 13, 3:24 pm, Rahul <sam_...@yahoo.co.in> wrote:
On Jan 13, 7:14 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
[...]
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...
Maybe, but there have been (and still are) implementations which
instantiate templates at link time.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: hpp |
13 Jan 2008 09:48:42 AM |
|
|
Rahul wrote:
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
.
|
|
|
|
|
|
|
|

|
Related Articles |
|
|