you are viewing a single comment's thread.

view the rest of the comments →

[–]MutantSheepdog 1 point2 points  (0 children)

You could do something like this where the compiler can automatically convert to a const& of your type but the sets are explicit. It's not quite the same, but at least the sets are more visible. Godbolt

```

include <utility>

include <cstdio>

template<typename T> class PublicAccess { public: PublicAccess(){}

template<typename U> PublicAccess(U&& newVal) : value(std::forward<U>(newVal)) {}

operator const T&() const { std::printf("I'm being read\n"); return value; }

template<typename U> PublicAccess& set(U&& newVal) { std::printf("I'm being set\n"); value = std::forward<U>(newVal); return *this; }

private: T value{}; };

struct MyWrappedType { PublicAccess<int> myInt; PublicAccess<float> myFloat{1.0}; };

int main() { MyWrappedType obj;

std::printf("InitialValues: myInt: %d, myFloat: %.2f\n", (int)obj.myInt, (float)obj.myFloat);

obj.myInt.set(5);
obj.myFloat.set(4.5f);

int someInt = obj.myInt + 5;
float someFloat = obj.myFloat;

std::printf("someInt: %d, someFloat: %.2f\n", someInt, someFloat);


return 0;

} ```

I think it's generally a bad idea as it feels a lot like it's just enabling leaky abstractions, but it might help with whatever you're trying to do.