C++模板 静态成员 定义(实例化)
问一个问题:
考虑一个模板:
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();
为了不影响大家分析推断,我把答案颜色调成比較浅的颜色,以下即是答案:
- template <> string Test<int>::info("123");//ok
- template <typename T> string Test<T>::info("123");//ok
- template <typename T> string Test<T>::info;//ok
- template <> string Test<int>::info; //error
- template <> string Test<int>::info();//error
- template <typename T> string Test<T>::info();//error
问题解答:
首先,说明一下三个正确的答案。
第一种形式称之为特化定义,其作用是为模板某一特化提供其静态成员的定义,在我们样例中,它只为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
版权声明:本文博主原创文章,博客,未经同意不得转载。
C++模板 静态成员 定义(实例化)的更多相关文章
- [转]C++ 模板 静态成员 定义(实例化)
提出问题: 如果有这样一个模板: template <typename T> class Test{ public: static std::string info; }; 对于以下若干种 ...
- C++ Primer 学习笔记_77_模板与泛型编程 --实例化
模板与泛型编程 --实例化 引言: 模板是一个蓝图,它本身不是类或函数.编译器使用模板产生指定的类或函数的特定版本号.产生模板的特定类型实例的过程称为实例化. 模板在使用时将进行实例化,类模板在引用实 ...
- 读书笔记 effective c++ Item 46 如果想进行类型转换,在模板内部定义非成员函数
1. 问题的引入——将operator*模板化 Item 24中解释了为什么对于所有参数的隐式类型转换,只有非成员函数是合格的,并且使用了一个为Rational 类创建的operator*函数作为实例 ...
- C++模板显式实例化,隐式实例化,特化(具体化,偏特化)辨析
最近再次看C++ PRIMER PLUS的时候看到这个部分感觉讲得很烂,前后口径不一致,所以写个辨析让自己明白的同时也希望对此不太清楚的朋友能搞懂. 总结一下,C++只有模板显式实例化(explici ...
- C++ 模板的编译 以及 类模板内部的实例化
在C++中.编译器在看到模板的定义的时候.并不马上产生代码,仅仅有在看到用到模板时,比方调用了模板函数 或者 定义了类模板的 对象的时候.编译器才产生特定类型的代码. 一般而言,在调用函数的时候,仅仅 ...
- c/c++模板的定义和实现分开的问题及其解决方案
注意c/c++模板的定义和实现- - 定义一个类一般都是在头文件中进行类声明,在cpp文件中实现,但使用模板时应注意目前的C ...
- Ext.net Calendar 控件在有模板页的时候,模板页定义了TD的样式造成日历控件的样式丢掉
Ext.net Calendar 控件在有模板页的时候,模板页定义了TD的样式造成日历控件的样式丢掉 解决方案 在本页面添加下面的样式 <style type="text/css&qu ...
- template <typename T>模板类定义
#include "stdafx.h"#include "iostream"#include <ctime>using namespace std; ...
- c++模板使用及实现模板声明定义的分离
c++模板是编译器构造具体实例类型的模型,使类型参数化,是泛型编程的基础,泛型就是独立于特定类型. 一.模板分为函数模板和类模板两种. 函数模板:template <class 形参名,clas ...
随机推荐
- p标签里面不要放div标签(块元素)
最好不要在p标签里面嵌套块级元素(如div Ul): <p>我来测试下<div>块元素</div>放在p标签的情况</p> <p>我来测试下 ...
- BootStrap-validator 使用记录(JAVA SpringMVC实现)
BootStrap 是一个强大的前面框架,它用优雅的方式解决了网页问题.最近正在使用其开发网站的表单验证,一点体会记录如下: 注:本文中借鉴了博客Franson 的文章<使用bootstrapv ...
- 服务器安装VMware ESXI5.5
一.VMware ESXI5.5 vSphere5.5 VIMSetup下载http://blog.sina.com.cn/s/blog_61c07ac50101gy64.html(1)VMware安 ...
- 在mac平台运行debug.exe
最近准备学习操作系统,想先复习一下汇编语言.因为用的是mac,而看的汇编教材(<汇编语言>王爽)使用到DOS下的debug,在网上搜了一圈发现,mac 也可以模拟运行debug. 先到网上 ...
- C/C++中的函数传值
一.运行如下程序段 #include <iostream> #include <string> #include <cstring> //strcpy #inclu ...
- 连接SQLite 创建ADO.net实体类
0.开发环境 win10,vs2013-x64 1.安装: sqlite-netFx451-setup-bundle-x86-2013-1.0.102.0.exe 注意事项:选在VisualStudi ...
- 以字符串形式读取github上.json文件
如下: https://github.com/hpu-spring87/ebooks/blob/master/update.json 如果直接用httpclient读取该URL地址,得到结果是这样的: ...
- Win7中,取消共享文件夹后有个小锁
用过windows7的朋友都知道,Windows 7 中设置某一个文件夹属性为共享后,文件夹的图标上就增加一个小锁图案.起到了一个标记作用,挺好的.但是即使你将该文件夹的共享功能取消后,该小锁图案还是 ...
- nginx 504 Gateway Time-out错误解决办法
我们经常会发现大量的nginx服务器访问时会提示nginx 504 Gateway Time-out错误了,下面我来总结了一些解决办法,有需要了解的同学可进入参考. 一般看来, 这种情况可能是由于ng ...
- Codeforces Round #276 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...