I wanted to wrap following line as function which takes parameter as a template type (to replace ConnectionManager for any other type):
std::shared_ptr<ConnectionManager> client = ConnectionManager::createClient<ConnectionManager>(broker, service_url);
So I created function that takes template parameter:
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;
}
when I try to compile I get following error:
error: expected primary-expression before ‘>’ token
std::shared_ptr<ClientType> client = ClientType::createClient<ClientType>(broker, url);
Kdevelop suggested following change:
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;
}
After this change code compiles and works.
createClient is defined in ConnectionManager parent as follows
template<class IMPCLASS>
static inline ::thrift::shared_ptr<IMPCLASS> createClient(const ::thrift::shared_ptr< ::thrift::TServiceBroker >& broker, const std::string& address)
I have two questions:
- I don't understand why I got compilation error
- I don't even know syntax that fixed the error
Please explain.