C ++ template static member definition (instantiation), static instance

Source: Internet
Author: User

C ++ template static member definition (instantiation), static instance
Raise questions:

If there is such a template:

template <typename T> class Test{public:    static std::string info;};

 


Which of the following definition methods are correct (through compilation )?

template <> string Test<int>::info("123");template <typename T> string Test<T>::info("123");template <typename T> string Test<T>::info;template <> string Test<int>::info; template <> string Test<int>::info();template <typename T> string Test<T>::info();

 


In order not to affect your analysis and judgment, I adjusted the answer color to a lighter color. below is the answer:

 

Answer:

First, describe the three correct answers.

The first form is called a special definition. Its function is to provide the definition of its static members for a specific template. In our example, it only defines the static member info of the Test <int> class. The single-parameter constructor is called for initialization.

The second form is similar to the definition of static members of common classes. Its function is to provide the definition of static members implicitly for all the features of the template in this compilation unit. In our example, use Test <int>, Test <float>, Test <char>... static member definitions are provided implicitly and single-parameter constructor Initialization is called.

The third form is the same as the second form. The only difference is that the default constructor is used for initialization.

Next, describe the three wrong answers.

In the first form, many people think it is correct and think it is initialized using the default constructor. However, the compiler will perform special processing on the special definition. Compilation considers it a declaration rather than a definition. Ask the person who makes the standard. I think it may be difficult to implement such a syntax, and it is also quite tricky.

The second form is not to declare a function.

The third form is the same as the second form.

More information:

What are the other differences between the two correct definitions?

//. Cpptemplate <typename T> string Test <T>: info ("4321"); you can use Test <int>: info // B. cpptemplate <typename T> string Test <T>: info ("1234"); you can also use Test <int>: info

 

The two definitions can coexist in different compilation units. Test <int>: The initial value of info depends on the initialization sequence of static members, so this is not a good thing.

//a.cpptemplate <> string Test<int>::info("123");//b.cpptemplate <> string Test<int>::info("123");

 

The special definition cannot be compiled.

//. Cpptemplate <> string Test <int>: info ("123"); // B. cpptemplate <typename T> string Test <T>: info ("123"); Once Test <int>: info is used, compilation fails.


The above method cannot be compiled.

 

Generally, to avoid compilation failures, we should try to reduce the usage as follows:

template <typename T> string Test<T>::info;

You only need to provide the following special definition in the implementation file when you first need to use it. Other files can be used as long as they contain header files.

template <> string Test<int>::info("123");
Application case:
/** Copyright (C) 2007 The Android Open Source Project ** Licensed under the Apache License, Version 2.0 (the "License "); * you may not use this file before t in compliance with the License. * You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on An "as is" BASIS, * without warranties or conditions of any kind, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */# ifndef ANDROID_UTILS_SINGLETON_H # define ANDROID_UTILS_SINGLETON_H # include <stdint. h> # include <sys/types. h> # include <utils/threads. h> # include <cutils/compiler. h> namespace android {//------------------ Extends template <typename TYPE> class ANDROID_API Singleton {public: static TYPE & getInstance () {Mutex: Autolock _ l (sLock); TYPE * instance = sInstance; if (instance = 0) {instance = new TYPE (); sInstance = instance;} return * instance;} static bool hasInstance () {Mutex :: autolock _ l (sLock); return sInstance! = 0;} protected :~ Singleton () {}; private: // prohibit copying constructor and value assignment operator functions. Disable calling declare private inside and outside the class, not define Singleton (const Singleton &); Singleton & operator = (const Singleton &); static Mutex sLock; static TYPE * sInstance ;};/** use ANDROID_SINGLETON_STATIC_INSTANCE (TYPE) in your implementation file * (eg: <TYPE>. cpp) to create the static instance of Singleton <>'s attributes, * and avoid to have a copy Of them in each compilation units Singleton <TYPE> * is used. ** NOTE: we use a version of Mutex ctor that takes a parameter, because * for some unknown reason using the default ctor doesn' t emit the variable! The special definition must use a constructor with parameters. Otherwise, it is declared! * // If you want to use Singleton, you must include this macro in the implementation file of the custom TYPE to initialize the static variable of the class template and display the instantiated class template # define ANDROID_SINGLETON_STATIC_INSTANCE (TYPE) \ template <> Mutex Singleton <TYPE >:: sLock (Mutex: PRIVATE); \ custom template <> TYPE * Singleton <TYPE >:: sInstance (0 ); \ special definition template class Singleton <TYPE>; \ Display instantiation // -------------------------------------------------------------------------------}; // namespace android

 



 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.