Skip to content

Instantly share code, notes, and snippets.

@Frityet
Created March 24, 2022 08:42
Show Gist options
  • Save Frityet/268106b5adec74d876563314f92b04af to your computer and use it in GitHub Desktop.
Save Frityet/268106b5adec74d876563314f92b04af to your computer and use it in GitHub Desktop.
template<typename T, T (*Get_F)(T *), void (*Set_F)(T *, T)>
class Property {
private:
T field_;
public:
explicit Property(T value)
{ this->field_ = value; }
[[nodiscard]]
T get() const
{ return Get_F(&(this->field_)); }
void set(T value) const
{ Set_F((T *)(&(this->field_)), value); }
Property<T, Get_F, Set_F> &operator =(T value)
{
this->set(value);
return *this;
}
operator T()
{ return this->field_; }
};
template<typename T, T (*Get_F)(T *) = [](T *field) {
return *field;
}, void (*Set_F)(T *, T) = [](T *field, T value) {
*field = value;
}>
using property = class Property<T, Get_F, Set_F>;
int main()
{
puts("Start");
auto prop = property<int,
[](int *i) {
printf("%d\n", *i);
return *i;
},
[](int *ptr, int i) {
printf("%d, %d\n", *ptr, i);
*ptr = i;
}>(64);
prop = 56;
putchar(prop);
return 0;
}
@Frityet
Copy link
Author

Frityet commented Mar 24, 2022

error:

FAILED: CMakeFiles/OoPenis.dir/main.cpp.o 
/usr/local/Cellar/gcc/11.2.0_3/bin/g++-11   -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -mmacosx-version-min=12.2 -std=gnu++23 -MD -MT CMakeFiles/OoPenis.dir/main.cpp.o -MF CMakeFiles/OoPenis.dir/main.cpp.o.d -o CMakeFiles/OoPenis.dir/main.cpp.o -c /Users/Frityet/Documents/OoPenis/main.cpp
/Users/Frityet/Documents/OoPenis/main.cpp: In function 'int main()':
/Users/Frityet/Documents/OoPenis/main.cpp:150:6: error: 'main()::<lambda(int*)>::_FUN' is not a valid template argument for type 'int (*)(int*)' because 'static constexpr int main()::<lambda(int*)>::_FUN(int*)' has no linkage
  150 |     }>(64);
      |      ^
/Users/Frityet/Documents/OoPenis/main.cpp:150:6: error: 'main()::<lambda(int*, int)>::_FUN' is not a valid template argument for type 'void (*)(int*, int)' because 'static constexpr void main()::<lambda(int*, int)>::_FUN(int*, int)' has no linkage
ninja: build stopped: subcommand failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment