template/type deduction and 'oneof' in message

468 views
Skip to first unread message

Alex Shaver

unread,
May 19, 2016, 3:42:52 PM5/19/16
to Protocol Buffers
Suppose I have a message:
message Foo{
  oneof b_message
{
   
Bar = 16;
   
Baz = 17;
 
}
}

and I have some C++ handler that is a templated function:
template<class T>
void b_handler(const T&);
template void b_handler(const Bar& bar){...}
template void b_handler(const Baz& baz){...}  

What I'd like to be able to do is:
void foo_handler(const Foo& foo){
 
...
  b_handler
(foo.b_message());
}  

Essentially, if protoc generated some kind of function like
template<class T>
const T& Foo::b_message() const;
const Bar& Foo::b_message() const;
const Baz& Foo::b_message() const;

that would make for automatic handling of whatever kind of b_message Foo had carried, rather than doing a switch(foo.b_message_case()) type decision tree.

I'm sure there are a lot of edge cases that I'm not thinking of, or cross-language considerations, but is anything like this possible?

Feng Xiao

unread,
May 19, 2016, 6:09:58 PM5/19/16
to Alex Shaver, Protocol Buffers
On Thu, May 19, 2016 at 10:31 AM, Alex Shaver <alexs...@gmail.com> wrote:
Suppose I have a message:
message Foo{
  oneof b_message
{
   
Bar = 16;
   
Baz = 17;
 
}
}

and I have some C++ handler that is a templated function:
template<class T>
void b_handler(const T&);
template void b_handler(const Bar& bar){...}
template void b_handler(const Baz& baz){...}  

What I'd like to be able to do is:
void foo_handler(const Foo& foo){
 
...
  b_handler
(foo.b_message());
}  

Essentially, if protoc generated some kind of function like
template<class T>
const T& Foo::b_message() const;
const Bar& Foo::b_message() const;
const Baz& Foo::b_message() const;

that would make for automatic handling of whatever kind of b_message Foo had carried, rather than doing a switch(foo.b_message_case()) type decision tree.
It's pretty easy to write a protoc plugin to generate additional code. What I don't understand though is how can you have two methods with the same signature return different types? Is that supported by C++?
 

I'm sure there are a lot of edge cases that I'm not thinking of, or cross-language considerations, but is anything like this possible?

--
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 https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Alex Shaver

unread,
May 19, 2016, 6:17:20 PM5/19/16
to Feng Xiao, Protocol Buffers

So, the cpprefernce page on template type deduction seemingly suggests you can: http://en.cppreference.com/w/cpp/language/template_argument_deduction

Adam Cozzette

unread,
May 19, 2016, 6:52:22 PM5/19/16
to Alex Shaver, Feng Xiao, Protocol Buffers
I don't think this kind of type deduction is possible here, because the type of the oneof can't be known until runtime and so the compiler has no way of knowing which function to select. Alex, I believe the convert example at the top of your link works but only because the caller explicitly specifies the return type, using e.g. convert<int>(d). Ultimately I would guess that you either need a switch statement or something like a std::map that maps each enum value to the right handler function taking const Foo& as its argument.
Reply all
Reply to author
Forward
0 new messages