Can protobuf work with C++ templates?

1,935 views
Skip to first unread message

Ji Wan

unread,
Jan 19, 2015, 8:41:25 PM1/19/15
to prot...@googlegroups.com
Suppose I have two message types `DoubleMatrix` and `FloatMatrix`, and a template class `Matrix`:


message DoubleMatrix {
  required uint32 rows
= 1;
  required uint32 cols
= 2;
  repeated
double data = 3 [packed=true];
}


message
FloatMatrix {
  required uint32 rows
= 1;
  required uint32 cols
= 2;
  repeated
float data = 3 [packed=true];
}


template<typename DType>
class Matrix {
   
MSTType mat_data_;
};



Is it possible to make `MSTType` as `FloatMatrix` if `DType` is `float`, as `DoubleMatrix` if `DType` is `double`?

Stephen Tu

unread,
Jan 21, 2015, 12:28:53 PM1/21/15
to Ji Wan, prot...@googlegroups.com
This is not really a protobuf question, moreso a C++ question. But anyways, the typical way to do this is:

template <typename T> struct MatrixTraits { };
template <> struct MatrixTraits<double> { typedef DoubleMatrix type; };
template <> struct MatrixTraits<float>    { typedef FloatMatrix type; };

template <typename DType> class Matrix { 
    typename MatrixTraits<DType>::type mat_data_;
};

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Ji Wan

unread,
Jan 21, 2015, 6:56:06 PM1/21/15
to Stephen Tu, prot...@googlegroups.com
Got a new skill :P
Thanks very much!

--- Original Message ---

Jui-Hsien Wang

unread,
May 17, 2019, 12:37:10 AM5/17/19
to Protocol Buffers
I am revoking this old question but I would like to know if protobuf can handle class templates?

Suppose I have a simple class
template<typename T>
struct Data {
    T data
;
};

and know that T can only take on float and double. Is there a way to avoid writing two proto files?




On Wednesday, January 21, 2015 at 9:28:53 AM UTC-8, Stephen Tu wrote:
This is not really a protobuf question, moreso a C++ question. But anyways, the typical way to do this is:

template <typename T> struct MatrixTraits { };
template <> struct MatrixTraits<double> { typedef DoubleMatrix type; };
template <> struct MatrixTraits<float>    { typedef FloatMatrix type; };

template <typename DType> class Matrix { 
    typename MatrixTraits<DType>::type mat_data_;
};
On Mon, Jan 19, 2015 at 5:41 PM, Ji Wan <wa...@live.com> wrote:
Suppose I have two message types `DoubleMatrix` and `FloatMatrix`, and a template class `Matrix`:


message DoubleMatrix {
  required uint32 rows
= 1;
  required uint32 cols
= 2;
  repeated
double data = 3 [packed=true];
}


message
FloatMatrix {
  required uint32 rows
= 1;
  required uint32 cols
= 2;
  repeated
float data = 3 [packed=true];
}


template<typename DType>
class Matrix {
   
MSTType mat_data_;
};



Is it possible to make `MSTType` as `FloatMatrix` if `DType` is `float`, as `DoubleMatrix` if `DType` is `double`?

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prot...@googlegroups.com.

Adam Cozzette

unread,
May 17, 2019, 4:40:59 PM5/17/19
to Jui-Hsien Wang, Protocol Buffers
No, there is not really any good alternative but to write two separate message definitions. The .proto file format is language-agnostic so it can't use any C++-specific features.

To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.

Jui

unread,
May 17, 2019, 4:43:39 PM5/17/19
to Protocol Buffers
Yeah this actually makes sense. I just want to make sure I did not miss anything. Thanks.

Reply all
Reply to author
Forward
0 new messages