“模板<class _tp,=”“ class =”“ _alloc =” __ STL_DEFAULT_ALLOCATOR(_Tp)“>” =是什么意思?

时间:2018-06-27 17:31:30

标签: c++

当我看到stl源代码时,我看到一些代码,我听不懂吗?

template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
class vector : protected _Vector_base<_Tp, _Alloc> 
{
  // requirements:

  __STL_CLASS_REQUIRES(_Tp, _Assignable);
  ....
}

那么,问题是=template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >中意味着什么?

我能像使用关键字一样理解=吗?

using _Alloc = class __STL_DEFAULT_ALLOCATOR(_Tp)

2 个答案:

答案 0 :(得分:2)

=表示默认模板参数。

例如

template <class Foo = std::string>
class Bar

表示“如果未提供任何参数,则使用std::string。您可以像这样使用Bar

Bar b;

等于

Bar<std::string> b;

就您而言,

template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
class vector

表示

  

默认情况下,如果未指定其他任何内容,请使用__STL_DEFAULT_ALLOCATOR(_TP)

允许您写

vector<int> v;

不必写

vector<int, __STL_DEFAULT_ALLOCATOR(int)> v;

这将是更详细的


有关更多详细信息,请参见this answer

答案 1 :(得分:1)

CREATE PROCEDURE sp_UpdateUserProfile @UserTabkeID INT, @Birthdate datetime AS BEGIN -- break it apart even more convert it first to Binary here, then encrypt it below before updating value DECLARE @ConvertedBirthday AS VARBINARY(MAX) SET @ConvertedBirthday = CONVERT(varbinary(max), @Birthdate) OPEN SYMMETRIC KEY SymmetricKey1 DECRYPTION BY CERTIFICATE Certificate1; -- just a possible solution to pull out the conversion from being inline so doing conversion and encryption ahead of time inside the decrypt key, then close the encrpt key, and use that variable in your update -- NOTE as I said this will not fix your encrypt issue, just make it easier to debug by breaking it apart DECLARE @EncryptedValue as Varbinary(MAX) SET @EncryptedValue = EncryptByKey(Key_GUID('SymmetricKey1'), @ConvertedBirthday) CLOSE SYMMETRIC KEY SymmetricKey1; UPDATE table1 SET Birthday_encrypt = @EncryptedValue Where table1.UserTableID = @UserTableID; END GO 表示模板接受参数class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp),如果不提供该参数,则将使用默认的_Alloc

__STL_DEFAULT_ALLOCATOR(_Tp)的意思是“默认使用”。