Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q: Implementing a member attribute for "Default value" to use in ctors #82

Open
FoamScience opened this issue Oct 7, 2023 · 0 comments

Comments

@FoamScience
Copy link

I'd like to maintain a certain level of verbosity when it comes to important class members. Currently, I'm writing members as:

class MyClass
{
    struct d {
        static constexpr std::string_view name_ = "d";
        static constexpr std::string_view descritption_ = "d";
        static const std::vector<double> default_;
        std::vector<double> v;
    } d;
};

This works, and with some macros, I can make it clutter-free; to some extent.

I was wondering if it is possible to write a reflected attribute to the member so it can be used as:

// d here is just a std::vector
REFL_AUTO(
    type(MyClass),
    field(d, defaulted<std::vector<double>>({2, 3.5, 10})),
)

And in constructor:
```cpp
// Use reflected default in ctor
MyClass::MyClass() : d(getDefault<MyClass>("d")){}

While this works for literal types; I don't seem to be able to make it work for types which require dynamic memory allocation. Any pointers as to what to do to make this work? Having the reflection system take care of this will be amazing.

Here is my attempt at a defaulted attribute which works for literal types: https://godbolt.org/z/44KG4YbxT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant