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.
On Thu, May 16, 2019 at 9:37 PM Jui-Hsien Wang <[email protected]> wrote: > 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 <[email protected]> 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 [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at http://groups.google.com/group/protobuf. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/protobuf. > To view this discussion on the web visit > https://groups.google.com/d/msgid/protobuf/056beefe-b2e1-4c79-8c44-b7027191eded%40googlegroups.com > <https://groups.google.com/d/msgid/protobuf/056beefe-b2e1-4c79-8c44-b7027191eded%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/protobuf. To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/CADqAXr7X%2B%3DbMPFyRJ3b2b0YOEFDaq_YD6SLL32k2eHMLmOr%3DdQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
