[转]C++ 模板 静态成员 定义(实例化)
提出问题:
如果有这样一个模板:
1 2 3 4 | template < typename T> class Test{ public : static std::string info; }; |
对于以下若干种定义方式,哪些是对的(通过编译)?
1 2 3 4 5 6 | 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>...会隐式提供静态成员的定义,并且调用单参 数构造函数初始化。
第三种形式和第二种形式一致,唯一不同就是采用默认构造函数初始化。
其次,说明一下三个错误的答案。
第一种形式,很多人都会认为是对的,认为它采用默认构造函数初始化。但编译器会对特化定义进行特殊处理,编译认为它是一个声明而非定义。至于为什么如此,应该询问一下制定标准的人。我认为可能实现这样的语法可能比较困难并且这个语法也比较鸡肋。
第二种形式,这不成了声明一个函数啦。
第三种形式,同第二种。
更多内容:
两种正确的定义方式还有哪些其他的区别呢?
1 2 3 4 5 6 | //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的初始值是多少,这取决与静态成员的初始化顺序,所以这不是件好事。
1 2 3 4 | //a.cpp template <> string Test< int >::info( "123" ); //b.cpp template <> string Test< int >::info( "123" ); |
而特化定义,上述方式无法通过编译。
1 2 3 4 5 6 7 | //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");
应用案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /* * 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++学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架