Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
read_write_lock_acc
#include <iostream> #include <algorithm> struct AB { int x = 1; double y = 100.; }; typedef int read_lock; typedef double write_lock; template<typename LockType, typename ABType> class X : public ABType{ public: typedef const ABType& access_type; }; template<typename ABType> class X<write_lock, ABType> : public ABType{ public: typedef ABType& access_type; }; template<typename LockType> struct XY:protected X<LockType, AB>{ template<typename WeightType> std::enable_if_t<std::is_floating_point<WeightType>::value, WeightType> value() const { return (WeightType)X<LockType, AB>::y; } template<typename WeightType> std::enable_if_t<std::is_integral<WeightType>::value, WeightType> value() const { return X<LockType, AB>::x; } typedef typename X<LockType, AB>::access_type access_type; operator access_type(){ return *this; } access_type data(){ return *this; } template<typename T = LockType> std::enable_if_t<std::is_same<T, read_lock>::value, const AB&> data(){return *this;} template<typename T = LockType> std::enable_if_t<std::is_same<T, write_lock>::value, AB&> data(){return *this;} }; int main(int, char**) { XY<write_lock> xy; std::cout << xy.value<float>() << std::endl << xy.value<short>() << std::endl; // AB& ab0 = xy; // <---------- why error here? // const AB& ab1 = xy.data(); AB& ab1 = xy.data(); AB& ab2 = xy.data<>(); return 0; }
run
|
edit
|
history
|
help
0
RegExpress
Articulation and Bridges
sdfg
Zadanie Kalkulator z bajerami
Dar
Lotto random number game {modify}
1568-1644 Solar eclipses, make 1617(8) and 1620(12) Lunar eclipses the same as Chinese records
Metodos
remove_copy-30-Seconds-of-C++
Vector Example