[英]Default template type gives error but explicit type does not
My CCustomerBulkRecordset
class is declared something like this: 我的CCustomerBulkRecordset
类声明如下:
template <class T = CRecordsetEx>
class CCustomerBulkRecordset : public T
{
// Member declarations
};
And I have a method in my CRemoteDatabase
class that looks like this: 我的CRemoteDatabase
类中有一个方法如下:
template<typename T = CRecordset>
std::unique_ptr<T> ExecuteSqlQuery(LPCTSTR pszSqlQuery, UINT nOpenType = AFX_DB_USE_DEFAULT_TYPE, DWORD dwOptions = CRecordset::none)
{
// ...
}
I can use the following code to to call that method and it works fine. 我可以使用以下代码来调用该方法,它可以正常工作。
CRemoteDatabase db;
db.Open();
auto prs = db.ExecuteSqlQuery<CCustomerBulkRecordset<CRecordset>>(NULL, CRecordset::forwardOnly, CRecordset::useMultiRowFetch);
But the following code gives me a compile errors. 但是下面的代码给了我一个编译错误。
CRemoteDatabase db;
db.Open();
auto prs = db.ExecuteSqlQuery<CCustomerBulkRecordset>(NULL, CRecordset::forwardOnly, CRecordset::useMultiRowFetch);
Error C2672 'CDatabaseCommon::ExecuteSqlQuery': no matching overloaded function found 错误C2672'CDatabaseCommon :: ExecuteSqlQuery':找不到匹配的重载函数
Error C3206 'CDatabaseCommon::ExecuteSqlQuery': invalid template argument for 'T', missing template argument list on class template 'CCustomerBulkRecordset' 错误C3206'CDatabaseCommon :: ExecuteSqlQuery':'T'的无效模板参数,类模板'CCustomerBulkRecordset'上缺少模板参数列表
The only difference is that I'm not specifying the CCustomerBulkRecordset
type name in the second one. 唯一的区别是,我没有在第二个中指定CCustomerBulkRecordset
类型名称。 But since it defaults to CRecordset
, shouldn't the two versions work the same? 但是由于它默认为CRecordset
,所以两个版本不应该一样工作吗?
In 在
template<typename T = CRecordset>
std::unique_ptr<T> ExecuteSqlQuery(...)
T
is a template parameter. T
是模板参数。 That means it must be deduced to a single type, or provided a type explicitly. 这意味着必须将其推导为单个类型,或显式提供一个类型。 When you do 当你做
auto prs = db.ExecuteSqlQuery<CCustomerBulkRecordset<CRecordset>>(...);
You give it the type CCustomerBulkRecordset<CRecordset>
. 您给它类型CCustomerBulkRecordset<CRecordset>
。 When you do 当你做
auto prs = db.ExecuteSqlQuery<CCustomerBulkRecordset>(...);
You are giving it CCustomerBulkRecordset
, which is not a type, but instead it is a template. 您给它CCustomerBulkRecordset
,它不是类型,而是模板。 In order to pass a template to a function you need to use a template template parameter . 为了将模板传递给函数,您需要使用模板template参数 。
The presence of the default template parameter does not turn CCustomerBulkRecordset
into CCustomerBulkRecordset<CRecordset>
. 默认模板参数的存在不会将CCustomerBulkRecordset
转换为CCustomerBulkRecordset<CRecordset>
。 In order to get that you need CCustomerBulkRecordset<>
as the <>
indicates you want to instantiate the template with the default value. 为了获得该信息,您需要CCustomerBulkRecordset<>
因为<>
表示您要使用默认值实例化模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.