Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22048

Braced-init style constructor for protobuf conversion

$
0
0

I have some C++11 structs and classes that I'm trying to back or replace with protobufs. I have a struct that's initialized in a few places using the aggregate initialization style, like this:

  SomeStruct instance ({
    "some string",
    {"an", "array", "of", "strings",},
    SomeEnum::SomeValue,
  });

If I try to replace SomeStruct with it's corresponding protobuf, it complains that there's "no matching constructor for initialization of" SomeStructProto and "cannot convert initializer list argument to" SomeStructProto. Which makes sense, since there's no constructor for this in the generated protobuf code and the fields are private.

I'd like to modify how the code is being used as little as possible. Is there a way to build a function or helper class that would work sort of like the proper constructor? Something like this:

  SomeStruct instance = SomeStructConversionFunction ({
    "some string",
    {"an", "array", "of", "strings",},
    SomeEnum::SomeValue,
  });

This is my attempt so far, just to see if I can get it to compile:

SomeStructProto SomeStructConversionFunction(
    std::initializer_list<SomeStructProto> config) {
  return SomeStructProto();
}

but the compiler complains "no known conversion from 'const char [1]' to 'SomeStructProto' for 1st argument".

I'm pretty flexible with how the end result would work, I just don't want to end up with lots of some_struct.set_some_field("some string"). Any ideas?


Viewing all articles
Browse latest Browse all 22048

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>