grpc-c++

Failed to initialize grpc::ClientReader (C2664 error)


Platform: Windows MSVC 2022 (v143)
C++ version: C++17
gRPC version: 1.60.0 (installed with vcpkg)

std::unique_ptr< grpc::ClientReader<MyData> > reader(
  stub->StreamService(&context, request)
);

It returns error C2664:

cannot convert parameter
'std::unique_ptrgrpc::ClientReader<MyData,std::default_deletegrpc::ClientReader<MyData>>::unique_ptr(const std::unique_ptrgrpc::ClientReader<MyData,std::default_deletegrpc::ClientReader<MyData>> &)': 1 from 'std::unique_ptrgrpc::ClientReaderInterface<MyData,std::default_deletegrpc::ClientReaderInterface<MyData>>' to 'const std::unique_ptrgrpc::ClientReader<MyData,std::default_deletegrpc::ClientReader<MyData>> &'

I simply followed the example code in grpc C++ Basics tutorial, and stuck in this problem.


Solution

  • Stub->StreamService(&context, request) returns a smart pointer of ClientReaderInterface.
    I tried to transfer ownership of this smart pointer to reader, which is type of ClientReader (child class of ClientReaderInterface), through std::move, but it failed. Instead, I succeeded with the method below.

    auto client_reader_interface 
      = stub->StreamService(&context, request);
    auto reader 
      = std::unique_ptr<grpc::ClientReader<MyData> >(
          static_cast<grpc::ClientReader<MyData>* >(
            client_reader_interface.release()
          )
        );