#include <memory> // for unique_ptr.
#include <type_traits>
template<typename value_type>
class unko
{
public:
unko(value_type const &val) : sumapo{ new value_type{ val } } {}
auto get() const -> value_type &
{
return *this->sumapo.get();
}
auto get() -> value_type &
{
static_assert(!std::is_const<value_type>::value, "kuso shite nero(unko must be const)");
return *this->sumapo.get();
}
private:
std::unique_ptr<value_type> sumapo;
};
auto main() -> int
{
{
unko<int> a{ 20 }; int &i1 = a.get();
unko<int> const b{ 20 }; int &i2 = b.get();
// unko<int const> c{ 20 }; int const &i3 = c.get();
unko<int const> const d{ 20 }; int const &i4 = d.get();
}
}
I2luY2x1ZGUgPG1lbW9yeT4JLy8gZm9yIHVuaXF1ZV9wdHIuCiNpbmNsdWRlIDx0eXBlX3RyYWl0cz4KCgp0ZW1wbGF0ZTx0eXBlbmFtZSB2YWx1ZV90eXBlPgpjbGFzcyB1bmtvCnsKcHVibGljOgoJdW5rbyh2YWx1ZV90eXBlIGNvbnN0ICZ2YWwpIDogc3VtYXBveyBuZXcgdmFsdWVfdHlwZXsgdmFsIH0gfSB7fQoKCWF1dG8gZ2V0KCkgY29uc3QgLT4gdmFsdWVfdHlwZSAmCgl7CgkJcmV0dXJuICp0aGlzLT5zdW1hcG8uZ2V0KCk7Cgl9CgoJYXV0byBnZXQoKSAtPiB2YWx1ZV90eXBlICYKCXsKCQlzdGF0aWNfYXNzZXJ0KCFzdGQ6OmlzX2NvbnN0PHZhbHVlX3R5cGU+Ojp2YWx1ZSwgImt1c28gc2hpdGUgbmVybyh1bmtvIG11c3QgYmUgY29uc3QpIik7CgkJcmV0dXJuICp0aGlzLT5zdW1hcG8uZ2V0KCk7Cgl9Cgpwcml2YXRlOgoJc3RkOjp1bmlxdWVfcHRyPHZhbHVlX3R5cGU+IHN1bWFwbzsKfTsKCmF1dG8gbWFpbigpIC0+IGludAp7Cgl7CgkJdW5rbzxpbnQ+IGF7IDIwIH07IGludCAmaTEgPSBhLmdldCgpOwoJCXVua288aW50PiBjb25zdCBieyAyMCB9OyBpbnQgJmkyID0gYi5nZXQoKTsKLy8JCXVua288aW50IGNvbnN0PiBjeyAyMCB9OyBpbnQgY29uc3QgJmkzID0gYy5nZXQoKTsKCQl1bmtvPGludCBjb25zdD4gY29uc3QgZHsgMjAgfTsgaW50IGNvbnN0ICZpNCA9IGQuZ2V0KCk7Cgl9Cn0K