我想将以下行包装为将参数作为模板类型的函数(将ConnectionManager替换为任何其他类型):
std::shared_ptr<ConnectionManager> client = ConnectionManager::createClient<ConnectionManager>(broker, service_url);
所以我创建了带模板参数的函数:
template<typename ClientType>
std::shared_ptr<ClientType> createClient(const std::shared_ptr<thrift::TServiceBroker>& broker, std::string url) {
std::shared_ptr<ClientType> client = ClientType::createClient<ClientType>(broker, url);
return client;
}
当我尝试编译时出现以下错误:
error: expected primary-expression before ‘>’ token
std::shared_ptr<ClientType> client = ClientType::createClient<ClientType>(broker, url);
Kdevelop建议进行以下更改:
template<typename ClientType>
std::shared_ptr<ClientType> createClient(const std::shared_ptr<thrift::TServiceBroker>& broker, std::string url) {
std::shared_ptr<ClientType> client = ClientType::template createClient<ClientType>(broker, url);
return client;
}
此更改后代码编译并运行。
createClient在ConnectionManager父级中定义如下
template<class IMPCLASS>
static inline ::thrift::shared_ptr<IMPCLASS> createClient(const ::thrift::shared_ptr< ::thrift::TServiceBroker >& broker, const std::string& address)
我有两个问题:
请解释。