考虑一个模板:
template <typename T> class Test{
public:
static std::string info;
};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();
首先,说明一下三个正确的答案。
第一种形式称之为特化定义,其作用是为模板某一特化提供其静态成员的定义,在我们样例中,它只为Test<int>类的静态成员info提供定义。而且调用单參数构造函数初始化。
另外一种形式类似于普通类的静态成员的定义方式,其作用是隐式地在该编译单元为模板的全部特化提供其静态成员的定义,在我们样例中,在首次使用Test<int>,Test<float>,Test<char>...会隐式提供静态成员的定义,而且调用单參数构造函数初始化。
第三种形式和另外一种形式一致,唯一不同就是採用默认构造函数初始化。
其次。说明一下三个错误的答案。
第一种形式。非常多人都会觉得是对的。觉得它採用默认构造函数初始化。但编译器会对特化定义进行特殊处理,编译觉得它是一个声明而非定义。至于为什么如此,应该询问一下制定标准的人。我觉得可能实现这种语法可能比較困难而且这个语法也比較鸡肋。
另外一种形式,这不成了声明一个函数啦。
第三种形式。同另外一种。
两种正确的定义方式还有哪些其它的差别呢?
//a.cpp
template <typename T> string Test<T>::info("4321");
能够使用Test<int>::info
//b.cpp
template <typename T> string Test<T>::info("1234");
也能够使用Test<int>::info
这两个定义能够在不同的编译单元共存。Test<int>::info的初始值是多少,这取决与静态成员的初始化顺序,所以这不是件好事。
//a.cpp
template <> string Test<int>::info("123");
//b.cpp
template <> string Test<int>::info("123");//a.cpp
template <> string Test<int>::info("123");
//b.cpp
template <typename T> string Test<T>::info("123");
一旦使用Test<int>::info无法通编译
一般为了避免无法编译,应当尽量降低使用,例如以下方式的定义
template <typename T> string Test<T>::info;仅仅有在你首次须要使用时在实现文件里给出例如以下特化定义就可以,其它文件仅仅要包括头文件就能使用。
template <> string Test<int>::info("123");
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except 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 {
// ---------------------------------------------------------------------------
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() { };
Singleton() { };
private:
//禁止复制构造函数和赋值运算符函数,禁止类外部和内部以及友元调用 declare private,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! 特化定义必须使用有參数的构造函数,否则觉得是声明。
*/
//想要使用Singleton,须要在自己定义类型的实现文件里包括此宏,用以初始化类模版static变量,并显示实例化类模版
#define ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) template<> Mutex Singleton< TYPE >::sLock(Mutex::PRIVATE); \ 特化定义
template<> TYPE* Singleton< TYPE >::sInstance(0); \ 特化定义
template class Singleton< TYPE >; \显示实例化
// ---------------------------------------------------------------------------
}; // namespace android版权声明:本文博主原创文章,博客,未经同意不得转载。
原文:http://www.cnblogs.com/hrhguanli/p/4850122.html