Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8465035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:55:39+00:00 2026-06-10T14:55:39+00:00

Consider the following construct: //! Templated singleton. /*! Template wrapper enforcing the singleton behavior.

  • 0

Consider the following construct:

//! Templated singleton.
/*!
    Template wrapper enforcing the singleton behavior.
*/
template <class T>
class TSingleton
{
private:    
    //! Singleton instance pointer.
    static T* instance;
    //! private constructor.
    TSingleton() { }
    //! private empty copy constructor.
    TSingleton(const TSingleton<T>& sourceObject) {}

public:
    //! Static singleton instance getter.
    static T* GetInstance()
    {
        if (instance == 0)
            instance = new T();
        return instance;
    }

};

template <class T> T* TSingleton<T>::instance = 0;

This template class and the definition of the static instance are written in the same header file. For a non-template class, this causes a link-time error due to multiple symbols being defined for the instance static member. It seems intuitive for this to happen with templates as well, thus one must separate the definition and put it in a .cpp file. But templates are usually declared and defined in header-like files. What is it that allows this syntax to be valid and functional for template classes?

There a wikipedia link on this, but it does not provide a clear explanation on what happens in case of template classes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:55:41+00:00Added an answer on June 10, 2026 at 2:55 pm

    This works because [basic.def.odr]/5 explicitly allowed templates to be duplicated:

    There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. …

    The requirements are quite lengthy, so I won’t duplicate them here, but essentially they state that each duplicate definition must be identical (otherwise the program has undefined behaviour).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following contrived example of a templated array definition: template <typename t, unsigned
Consider following class of custom array template <typename element, unsigned int size> class array
Consider the following PHP code: <?php require_once(myDBclass.php); class a { private $tablename; private $column;
Let's consider following class template of custom array in Microsoft Visual C++ (Microsoft Visual
Consider the following code : template<typename T> class Base { Base(); Base(const Base<T>& rhs);
Consider following 2 programs giving same error First calss: public class Testing { Testing
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following code /* * To change this template, choose Tools | Templates
Consider the following code: class myclass { function __construct(&$arg1, &$arg2) { echo $arg1; echo
Consider the following constructor for the class Foo (which for the sake of clarity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.