原文地址:
今天在看vector.h的時候,碰到一個using的奇怪用法,才疏學淺之前沒有碰到過,整理一下。
來看下source code:
template<class _Ty,
class _Alloc = allocator<_Ty>>
class vector
: public _Vector_alloc<_Vec_base_types<_Ty, _Alloc>>
{ // varying size array of values
private:
using _Mybase = _Vector_alloc<_Vec_base_types<_Ty, _Alloc>>;
using _Alty = typename _Mybase::_Alty;
using _Alty_traits = typename _Mybase::_Alty_traits;
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same<_Ty, typename _Alloc::value_type>::value,
_MISMATCHED_ALLOCATOR_MESSAGE("vector<T, Allocator>", "T"));
using value_type = _Ty;
using allocator_type = _Alloc;
using pointer = typename _Mybase::pointer;
using const_pointer = typename _Mybase::const_pointer;
using reference = _Ty&;
using const_reference = const _Ty&;
using size_type = typename _Mybase::size_type;
using difference_type = typename _Mybase::difference_type;
using iterator = typename _Mybase::iterator;
using const_iterator = typename _Mybase::const_iterator;
using reverse_iterator = _STD reverse_iterator<iterator>;
using const_reverse_iterator = _STD reverse_iterator<const_iterator>;
下面來整理using的三種用法。
1、命名空間的使用
一般為了代碼的沖突,都會用命名空間。例如,對於Android代碼會使用Android作為命名空間。
namespace android;
在code中使用的時候可以用android::加具體的類方法。也可以直接使用using namespace android;
具體的命名空間使用方法不做過多說明。
2、在類中的作用
(1)在子類中引用基類的成員
來看下source code:
class T5Base {
public:
T5Base() :value(55) {}
virtual ~T5Base() {}
void test1() { cout << "T5Base test1..." << endl; }
protected:
int value;
};
class T5Derived : private T5Base {
public:
//using T5Base::test1;
//using T5Base::value;
void test2() { cout << "value is " << value << endl; }
};
基類中成員變量value是protected,在private繼承之后,對於外界這個值為private,也就是說T5Derived的對象無法使用這個value。
如果想要通過對象使用,需要在public下通過using T5Base::value來引用,這樣T5Derived的對象就可以直接使用。
同樣的,對於基類中的成員函數test1(),在private繼承后變為private,T5Derived的對象同樣無法訪問,通過using T5Base::test1 就可以使用了。
注意,using只是引用,不參與形參的指定。
(2)繼承中的作用
改變訪問權限
class Base
{
public:
void func() { cout << "Hello World" << endl;}
}
// private繼承會導致 func的可見性為private
// 可使用using,改變訪問權限
class Sub : private Base
{
public:
using Base::func;
}
繼承時子類重寫父類方法
子類中如果想重寫父類函數,比如名為func的函數。並且,假設父類中關於func函數有不同的版本。如果,子類中重寫一個函數,就會隱藏父類中所有同名函數。此時,只能通過顯示方式調用父類的方法。
即使,子類重新父類中的虛函數(覆蓋),也會導致該問題。
class Base
{
public:
void func() { // some code}
int func(int n) { // some code}
}
class Sub : public Base
{
public:
// 此處函數定義,會隱藏父類中 int func(int)方法。
void func() { // some code}
}
int main()
{
Sub s;
s.func();
s.func(1); // Error!
}
因此,如果不想隱藏父類的同名方法,可以重寫所有同名函數。這未免有些麻煩。使用using關鍵字,即可解決該問題。
class Base
{
public:
void func() { // some code}
int func(int n) { // some code}
}
class Sub : public Base
{
public:
using Base::func;
void func() { // some code}
}
int main()
{
Sub s;
s.func();
s.func(1); // Success!
}
3、別名指定
這點就是最開始看到的source code。在C++11中提出了通過using指定別名。
例如上面source code 中:
using value_type = _Ty
以后使用value_type value; 就代表_Ty value;
這個讓我們想起了typedef,using 跟typedef有什么區別呢?哪個更好用些呢?
例如:
typedef std::unique_ptr<std::unordered_map<std::string, std::string>> UPtrMapSS;
而C++11中:
using UPtrMapSS = std::unique_ptr<std::unordered_map<std::string, std::string>>;
或許從這個例子中,我們是看不出來明顯的好處的(而於我來說,以一個第三者的角度,這個例子也難以說服我一定要用C++11的using)。
再來看下:
typedef void (*FP) (int, const std::string&);
若不是特別熟悉函數指針與typedef的童鞋,我相信第一眼還是很難指出FP其實是一個別名,代表着的是一個函數指針,而指向的這個函數返回類型是void,接受參數是int, const std::string&。那么,讓我們換做C++11的寫法:
using FP = void (*) (int, const std::string&);
我想,即使第一次讀到這樣代碼,並且知道C++11 using的童鞋也能很容易知道FP是一個別名,using的寫法把別名的名字強制分離到了左邊,而把別名指向的放在了右邊,比較清晰。
而針對這樣的例子,我想我可以再補充一個例子:
typedef std::string (Foo::* fooMemFnPtr) (const std::string&);
using fooMemFnPtr = std::string (Foo::*) (const std::string&);
從可讀性來看,using也是要好於typedef的。
那么,若是從可讀性的理由支持using,力度也是稍微不足的。來看第二個理由,那就是舉出了一個typedef做不到,而using可以做到的例子:alias templates, 模板別名。
template <typename T>
using Vec = MyVector<T, MyAlloc<T>>;
// usage
Vec<int> vec;
這一切都會非常的自然。
那么,若你使用typedef來做這一切:
template <typename T>
typedef MyVector<T, MyAlloc<T>> Vec;
// usage
Vec<int> vec;
當你使用編譯器編譯的時候,將會得到類似:error: a typedef cannot be a template的錯誤信息。
那么,為什么typedef不可以呢?在 n1449 中提到過這樣的話:"we specifically avoid the term “typedef template” and introduce the new syntax involving the pair “using” and “=” to help avoid confusion: we are not defining any types here, we are introducing a synonym (i.e. alias) for an abstraction of a type-id (i.e. type expression) involving template parameters." 所以,我認為這其實是標准委員會他們的觀點與選擇,在C++11中,也是完全鼓勵用using,而不用typedef的。
具體的可以看下Effective Modern C++
參考:
https://blog.csdn.net/zxc024000/article/details/79438539
https://zhuanlan.zhihu.com/p/21264013