I have some structs that all have some common functionalities that I've put in an templated base class (here Foo<int>
)
template<int I>
struct Foo
{
void foo() { std::cout << "foo: " << I << '\n'; }
};
struct Bar : Foo<1>
{
int i;
int j;
};
Now when I initialize any of them I have to do it like so:
Bar b{{}, 1, 2};
And I would like to be able to do it like so:
Bar b{1, 2};
And also without the boilerplate of having to write a constructor. Is there some solution to this problem?
A similar question was asked here, however, this was over 7 years ago, so I was wondering if there were any developments since then.
Also, that question is marked duplicate of a question here, however this one is slightly different, as the base struct is not empty.
In addition, I'm not really trying to accomplish anything other than adding functionality to some structures, in a more declarative way, rather than repeating the definitions of all the common methods in each of them. So any solution that achieves that, by circumventing inheritance (apart from macros ideally ;D) would work as well.