![]() |
|
|
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. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|