Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-20-2008, 11:28 PM
DavidA
 
Posts: n/a
Question regarding a Singleton template

Hi

I am writing a C++ Singleton template based on some of the examples I
have seen. There is one detail I do not understand. In one example I
have seen, the Singleton 'instance' pointer is declared in a .h file
as follows:

//! static class member initialisation.
template <typename T> T* CSingleton<T>::m_instance = NULL;

Now, in a non-templated Singleton, I would expect the instance pointer
to be declared in the .cpp file. Then, only one instance of the
pointer will be created by the compiler. I can understand that in the
templated case, one needs this declaration with the template code in
the .h file, but what stops the compiler from creating an instance of
the pointer for every inclusion of the header file?

BR

David

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #2 (permalink)  
Old 08-21-2008, 09:56 PM
Martin Bonner
 
Posts: n/a
Re: Question regarding a Singleton template

On Aug 20, 11:28 pm, DavidA <dandbn...@talktalk.net> wrote:
> Hi
>
> I am writing a C++ Singleton template based on some of the examples I
> have seen. There is one detail I do not understand. In one example I
> have seen, the Singleton 'instance' pointer is declared in a .h file
> as follows:
>
> //! static class member initialisation.
> template <typename T> T* CSingleton<T>::m_instance = NULL;
>
> Now, in a non-templated Singleton, I would expect the instance pointer
> to be declared in the .cpp file. Then, only one instance of the
> pointer will be created by the compiler. I can understand that in the
> templated case, one needs this declaration with the template code in
> the .h file,


Exactly.
> but what stops the compiler from creating an instance of
> the pointer for every inclusion of the header file?


Magic. Or more exactly, the standard requires that the implementation
(which covers compiler+linker, or however else it is done) makes sure
that there is only one pointer with a well defined address.

This is actually almost the same question as "what stops the compiler
from creating an instance of the [member function] for every inclusion
of the header file?"

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #3 (permalink)  
Old 08-22-2008, 07:47 AM
ta0kira@yahoo.com
 
Posts: n/a
Re: Question regarding a Singleton template

On Aug 21, 4:56 pm, Martin Bonner <martinfro...@yahoo.co.uk> wrote:

> This is actually almost the same question as "what stops the compiler
> from creating an instance of the [member function] for every inclusion
> of the header file?"


No, not really. You'd get a "multiple definition" error if you
defined your functions outside of the class' body but still in the
header, then compiled and linked together several .cpp files which
include that header. Functions defined within the class' own
definition are implicitly inlined, which is why it isn't a problem in
those cases.

The compiler merges the definitions of template functions because
there's no way around having them defined within the same context as
they're used in. This is because the compiler only instantiates
specific templates when they're used with specific parameters,
otherwise you'd have an infinitely-large object file with every
possible instantiation of every template.
Kevin P. Barry

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #4 (permalink)  
Old 08-22-2008, 02:02 PM
Martin Bonner
 
Posts: n/a
Re: Question regarding a Singleton template

On Aug 22, 7:47 am, ta0k...@yahoo.com wrote:
> On Aug 21, 4:56 pm, Martin Bonner <martinfro...@yahoo.co.uk> wrote:
>
> > This is actually almost the same question as "what stops the compiler
> > from creating an instance of the [member function] for every inclusion
> > of the header file?"

>
> No, not really. You'd get a "multiple definition" error if you
> defined your functions outside of the class' body but still in the
> header, then compiled and linked together several .cpp files which
> include that header. Functions defined within the class' own
> definition are implicitly inlined, which is why it isn't a problem in
> those cases.


I think you have missed my point (which wasn't as clear as it could
be). How does an implementation merge the multiple definitions of an
inline function (one per TU)? It can use the same (or similar) magic
to merge the multiple definitions of a static member of a template
class.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #5 (permalink)  
Old 08-22-2008, 03:28 PM
Martin T
 
Posts: n/a
Re: Question regarding a Singleton template

ta0kira@yahoo.com wrote:
>> (...)

> (...) Functions defined within the class' own
> definition are implicitly inlined, which is why it isn't a problem in
> those cases.
>

Hmm, so in ...
struct A {
/*inline*/ void foo() {
// ...
}
};
.... foo() will always be inlined, no matter how large it is and no
matter if I specify inline??

> The compiler merges the definitions of template functions because
> there's no way around having them defined within the same context as
> they're used in. This is because the compiler only instantiates
> specific templates when they're used with specific parameters,
> otherwise you'd have an infinitely-large object file with every
> possible instantiation of every template.
>

So, given a template function, will it be always inline or will there be
one version of the instaciated template per translation unit where it's
used or what?

cheers,
Martin

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #6 (permalink)  
Old 08-23-2008, 01:58 AM
Greg Herlihy
 
Posts: n/a
Re: Question regarding a Singleton template

On Aug 20, 3:28 pm, DavidA <dandbn...@talktalk.net> wrote:
> I am writing a C++ Singleton template based on some of the examples I
> have seen. There is one detail I do not understand. In one example I
> have seen, the Singleton 'instance' pointer is declared in a .h file
> as follows:
>
> //! static class member initialisation.
> template <typename T> T* CSingleton<T>::m_instance = NULL;
>
> Now, in a non-templated Singleton, I would expect the instance pointer
> to be declared in the .cpp file. Then, only one instance of the
> pointer will be created by the compiler. I can understand that in the
> templated case, one needs this declaration with the template code in
> the .h file, but what stops the compiler from creating an instance of
> the pointer for every inclusion of the header file?


The reason why there is only one CSingleton<T>::m_instance static
variable instantiated in the entire program is straightforward: C++
templates are not instantiated within translation units. Instead, each
template is instantiated within its own "instantiation unit" (which
roughly corresponds to a translation unit).

Therefore, all of CSingleton's instantiated methods and instantiated
static class objects are placed in a CSingleton instantiation unit -
thereby ensuring that only one CSingleton::m_instance is allocated
(per CSingleton instantiation) for the entire program. (§2.1/8 of the C
++ Standard describes instantiation units).

Greg



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #7 (permalink)  
Old 08-24-2008, 12:58 AM
ta0kira@yahoo.com
 
Posts: n/a
Re: Question regarding a Singleton template

On Aug 22, 9:02 am, Martin Bonner <martinfro...@yahoo.co.uk> wrote:

> I think you have missed my point (which wasn't as clear as it could
> be). How does an implementation merge the multiple definitions of an
> inline function (one per TU)? It can use the same (or similar) magic
> to merge the multiple definitions of a static member of a template
> class.


I understand your point, yet inlined functions normally *don't have*
definiti unless you indicate that they should to the compiler. The
code is essentially just inserted as if they were macros, but with a
few differences.
Kevin P. Barry

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #8 (permalink)  
Old 08-24-2008, 12:58 AM
ta0kira@yahoo.com
 
Posts: n/a
Re: Question regarding a Singleton template

On Aug 22, 10:28 am, Martin T <0xCDCDC...@gmx.at> wrote:

> ... foo() will always be inlined, no matter how large it is and no
> matter if I specify inline??


That's normally the case because you'd risk having to rearrange your
file structure every time you added a new line to the function
otherwise.

> So, given a template function, will it be always inline or will there be
> one version of the instaciated template per translation unit where it's
> used or what?


C++ name mangling is a mess as it is, so most template instantiations
have such convoluted symbol names that it's pretty obvious to the
linker which ones are the same, though e.g. g++ won't catch it if the
two definitions differ (which would only happen if you defined it in
two places.) Template functions generally follow the same inlining
rules as all other functions.
Kevin P. Barry

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #9 (permalink)  
Old 08-24-2008, 12:58 AM
Francis Glassborow
 
Posts: n/a
Re: Question regarding a Singleton template

Martin T wrote:
> ta0kira@yahoo.com wrote:
>>> (...)

>> (...) Functions defined within the class' own
>> definition are implicitly inlined, which is why it isn't a problem in
>> those cases.
>>

> Hmm, so in ...
> struct A {
> /*inline*/ void foo() {
> // ...
> }
> };
> ... foo() will always be inlined, no matter how large it is and no
> matter if I specify inline??


NO!!

inline is not an instruction but a suggestion. The compiler is free to
inline any function it knows it can and to not inline any function that
it chooses not to. Of course compilers actually make the choices in
order to meet the coders 'needs' and do not just arbitrarily inline code
on a whim.

The only requirement of inlined code is that it can be defined in
multiple TU's without breaching the ODR. In effect this means that if
the code is not inlined the compiler must ensure that there is only one
effective translated copy per program (i.e. it must have a way to
conflate multiple instantiations.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #10 (permalink)  
Old 08-25-2008, 03:57 AM
Martin Bonner
 
Posts: n/a
Re: Question regarding a Singleton template

On Aug 24, 12:58 am, ta0k...@yahoo.com wrote:
> On Aug 22, 9:02 am, Martin Bonner <martinfro...@yahoo.co.uk> wrote:
>
> > I think you have missed my point (which wasn't as clear as it could
> > be). How does an implementation merge the multiple definitions of an
> > inline function (one per TU)? It can use the same (or similar) magic
> > to merge the multiple definitions of a static member of a template
> > class.

>
> I understand your point, yet inlined functions normally *don't have*
> definitions unless you indicate that they should to the compiler. The
> code is essentially just inserted as if they were macros, but with a
> few differences.


No. That's not true at all. Firstly
inline int foo() { return 0; }
is a definition of foo. How much code it generates (if any) is not
something the standard discusses.

Secondly, the /only/ difference that the 'inline' keyword makes to the
semantics of the abstract machine which the C++ standard deals with is
that it allows multiple definitions of the same function.

What you are describing as "code inserted as if they were macros" is
an optimization. Depending on the compiler and the options, a
compiler may:
- inline expand no functions and generate out of line code for all of
them
- inline expand some functions marked as 'inline' (but not all)
- inline expand some functions whether or not they are marked as
'inline'


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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



All times are GMT. The time now is 02:18 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Credit Card | Loans | Electricity Suppliers | Mortgage | Free Advertising



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114