模板函数无法编译:'>'之前的预期primary-expression

时间:2017-11-23 16:03:50

标签: c++ templates template-meta-programming

我想将以下行包装为将参数作为模板类型的函数(将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)

我有两个问题:

  1. 我不明白为什么会出现编译错误
  2. 我甚至不知道修复错误的语法
  3. 请解释。

0 个答案:

没有答案