Singleton with template [duplicate] - c++

This question already has answers here:
`auto` specifier type deduction for references
(2 answers)
Closed 5 years ago.
#include<iostream>
#include<mutex>
#include<stdlib.h>
using namespace std;
template<typename T>
class Singleton {
public:
static T& getInstance() {
if (instance_ == nullptr){
std::lock_guard<mutex> guard(mutex_);
if (instance_ == nullptr) {
instance_ = new T();
atexit(&Singleton::destroy);
}
}
return *instance_;
}
private:
static void destroy() {
delete instance_;
instance_ = nullptr;
}
Singleton() {}
Singleton(const Singleton&) {}
Singleton& operator=(const Singleton&) {}
static T* instance_;
static mutex mutex_;
};
template<typename T>
T* Singleton<T>::instance_ = nullptr;
template<typename T>
mutex Singleton<T>::mutex_;
class Test {
public:
Test() {
i_ = 0;
cout << "Construct Test" << endl;
}
void setValue(int&& x) {
i_ = x;
}
int getValue() {
return i_;
}
private:
int i_;
};
void main(){
auto instance1 = Singleton<Test>::getInstance();
auto instance2 = Singleton<Test>::getInstance();
instance1.setValue(2);
cout << instance2.getValue() << endl;
cout << instance1.getValue() << endl;
system("pause");
}
Why the type of instance1 and instance2 is Test not Test& ? "Construct Test" print only once, but the result is 0 and 2, instancen1 and instance2 seems two different Test object, the Environment is vs2015

Why the type of instance1 and instance2 is Test not Test& ?
Because you would need to use auto& in place of auto
auto& instance1 = Singleton<Test>::getInstance();
auto& instance2 = Singleton<Test>::getInstance();
You could verify that you're copying instead of getting a reference to the singleton variable with a copy constructor
Test(const Test&) {
cout << "Copying";
}
The rule is
int i = 5;
auto a1 = i; // value
auto & a2 = i; // reference
I'd also pay attention to Baum mit Augen's comment and avoid using void main and stdlib.h in the first place.

Related

How to pass static method of the class as deleter for std::unique_ptr?

I have been trying to pass a static method of following class as deleter of unique_ptr but I couldn't figure out the correct syntax.
Please excuse if the below syntax is horribly wrong as I just started hands on with unique_ptr.
#include <iostream>
#include <memory>
class Singleton {
public:
static void deleter(Singleton* p) {
delete p;
std::cout << "In deleter\n" << std::endl;
}
static std::unique_ptr<Singleton, decltype(&Singleton::deleter)>& getInstance(void);
void hello (void) {
std::cout << "Hello!\n" << std::endl;
}
Singleton(const Singleton&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(const Singleton&) = delete;
Singleton& operator=(Singleton&&) = delete;
private:
static std::unique_ptr<Singleton, decltype(&Singleton::deleter)> s_instance_;
Singleton() { std::cout << "Constructed\n" << std::endl; }
~Singleton() { std::cout << "Destroyed\n" << std::endl; }
};
std::unique_ptr<Singleton, decltype(&Singleton::deleter)>&
Singleton::getInstance (void)
{
if (s_instance_ == nullptr) {
s_instance_ = std::unique_ptr<Singleton, decltype(&Singleton::deleter)>(new Singleton(), &Singleton::deleter);
}
return s_instance_;
}
std::unique_ptr<Singleton, decltype(&Singleton::deleter)> Singleton::s_instance_{nullptr};
int main ()
{
bool some_condition = true;
std::unique_ptr<Singleton, decltype(&Singleton::deleter)> &ins = Singleton::getInstance();
ins->hello();
if (some_condition) {
ins.reset();
}
std::cout << "End of main\n" << std::endl;
return 0;
}
This gives following error with g++ (--version == g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609)
In file included from /usr/include/c++/5/memory:81:0,
from uptr.cpp:2:
/usr/include/c++/5/bits/unique_ptr.h: In instantiation of ‘constexpr std::unique_ptr<_Tp, _Dp>::unique_ptr() [with _Tp = Singleton; _Dp = void (*)(Singleton*)]’:
/usr/include/c++/5/bits/unique_ptr.h:200:61: required from ‘constexpr std::unique_ptr<_Tp, _Dp>::unique_ptr(std::nullptr_t) [with _Tp = Singleton; _Dp = void (*)(Singleton*); std::nullptr_t = std::nullptr_t]’
uptr.cpp:44:89: required from here
/usr/include/c++/5/bits/unique_ptr.h:159:9: error: static assertion failed: constructed with null function pointer deleter
{ static_assert(!is_pointer<deleter_type>::value,
^
Also, is there a way I can shorten std::unique_ptr<Singleton, decltype(&Singleton::deleter)> ? I tried using following approach before class definition but the compiler errors out for incomplete type:
using SingletonUPtr = std::unique_ptr<Singleton, decltype(&Singleton::deleter)>;
There is no need to store both type and value. The type can do the dispatching.
In c++11 you can just write this toy:
template<class F, F f>
struct call_t {
constexpr operator F() const{ return f; }
};
which magically works. (When you invoke it with (), C++ looks for a conversion to function pointer). This is an implementation of part of std::integral_constant with features missing in c++11. In this case, I am using it as a type that calls a constant evaluated value f of type F. It has other (similar) uses.
Try this:
void foo() { std::cout << "hello world\n"; }
int main() {
using foo_t = call_t< void(*)(), foo >;
foo_t foo_v = {};
foo_v(); // hello world
foo_t{}(); // hello world
std::cout << sizeof(foo_t) << "\n"; // 1, object is stateless
std::cout << sizeof(&foo) << "\n"; // 4 or 8, pointer has state
}
The rest then looks like:
class Singleton {
public:
static void deleter(Singleton* p) {
delete p;
std::cout << "In deleter\n" << std::endl;
}
using upSingleton = std::unique_ptr<Singleton, call_t<decltype(&Singleton::deleter), &Singleton::deleter> >;
static upSingleton& getInstance(void);
void hello() {
std::cout << "Hello!\n" << std::endl;
}
Singleton(const Singleton&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(const Singleton&) = delete;
Singleton& operator=(Singleton&&) = delete;
private:
static upSingleton s_instance_;
Singleton() { std::cout << "Constructed\n" << std::endl; }
~Singleton() { std::cout << "Destroyed\n" << std::endl; }
};
Singleton::upSingleton&
Singleton::getInstance()
{
if (s_instance_ == nullptr) {
s_instance_ = upSingleton(new Singleton());
}
return s_instance_;
}
Singleton::upSingleton Singleton::s_instance_{nullptr};
int main()
{
bool some_condition = true;
Singleton::upSingleton &ins = Singleton::getInstance();
ins->hello();
if (some_condition) {
ins.reset();
}
std::cout << "End of main\n" << std::endl;
return 0;
}
Live example.
The error is on line 44, which seems to be the definition for the Singleton::s_instance_ variable. You don't provide the deleter function there. You should include it in the definition, like you do earlier in getInstance.
std::unique_ptr<Singleton, decltype(&Singleton::deleter)> Singleton::s_instance_{nullptr, &Singleton::deleter};

Doubleton class - transition from single to double

Below I managed to create a singleton class that allows the instantiation of an object.
How can I modify the code as simple as possible to get a "Doubleton", to have two objects?
#include<iostream>
using namespace std;
class Singleton
{
public:
static Singleton& instance()
{
return uniqueInstance;
}
int getValue() { return data;}
void setValue(int value) { data = value;}
private:
static Singleton uniqueInstance;
int data;
Singleton(int d = 0):data(d) { }
Singleton & operator=(Singleton & ob){
if (this != &ob) data = ob.data; return *this; }
Singleton(const Singleton & ob) { data = ob.data; }
};
Singleton Singleton::uniqueInstance (0);
int main()
{
Singleton& s1 = Singleton::instance();
cout << s1.getValue() << endl;
Singleton& s2 = Singleton::instance();
s2.setValue(9);
cout << s1.getValue() <<endl;
return 0;
}
"Templetize" it:
#include<iostream>
using namespace std;
template<int N>
class Singleton {
public:
static Singleton& instance() {
return uniqueInstance;
}
int getValue() const { return data; }
void setValue(int value) { data = value; }
private:
static Singleton uniqueInstance;
int data;
Singleton(int d = 0) : data(d) {}
Singleton& operator=(Singleton& ob) {
if (this != &ob) data = ob.data;
return *this;
}
Singleton(const Singleton& ob) { data = ob.data; }
};
template<int N>
Singleton<N> Singleton<N>::uniqueInstance(0);
int main() {
Singleton<0>& s1 = Singleton<0>::instance();
cout << s1.getValue() << endl;
Singleton<1>& s2 = Singleton<1>::instance();
s2.setValue(9);
cout << s2.getValue() << endl;
return 0;
}
But to be honest, this is an anti-pattern if you need to do this I'd suggest reviewing the architecture...

Is there a way to have a public member, unmodifiable from outside the class, without accessor wrapper function?

As far as I know, this seems to be impossible in a straightforward way. Making the member const makes it const for everyone. I would like to have a read-only property, but would like to avoid the typical "getter". I'd like const public, mutable private. Is this at all possible in C++?
Currently all I can think of is some trickery with templates and friend. I'm investigating this now.
Might seem like a stupid question, but I have been surprised by answers here before.
A possible solution can be based on an inner class of which the outer one is a friend, like the following one:
struct S {
template<typename T>
class Prop {
friend struct S;
T t;
void operator=(T val) { t = val; }
public:
operator const T &() const { return t; }
};
void f() {
prop = 42;
}
Prop<int> prop;
};
int main() {
S s;
int i = s.prop;
//s.prop = 0;
s.f();
return i, 0;
}
As shown in the example, the class S can modify the property from within its member functions (see S::f). On the other side, the property cannot be modified in any other way but still read by means of the given operator that returns a const reference to the actual variable.
There seems to be another, more obvious solution: use a public const reference member, pointing to the private, mutable, member. live code here.
#include <iostream>
struct S {
private:
int member;
public:
const int& prop;
S() : member{42}, prop{member} {}
S(const S& s) : member{s.member}, prop{member} {}
S(S&& s) : member(s.member), prop{member} {}
S& operator=(const S& s) { member = s.member; return *this; }
S& operator=(S&& s) { member = s.member; return *this; }
void f() { member = 32; }
};
int main() {
using namespace std;
S s;
int i = s.prop;
cout << i << endl;
cout << s.prop << endl;
S s2{s};
// s.prop = 32; // ERROR: does not compile
s.f();
cout << s.prop << endl;
cout << s2.prop << endl;
s2.f();
S s3 = move(s2);
cout << s3.prop << endl;
S s4;
cout << s4.prop << endl;
s4 = s3;
cout << s4.prop << endl;
s4 = S{};
cout << s4.prop << endl;
}
I like #skypjack's answer, but would have written it somehow like this:
#include <iostream>
template <class Parent, class Value> class ROMember {
friend Parent;
Value v_;
inline ROMember(Value const &v) : v_{v} {}
inline ROMember(Value &&v) : v_{std::move(v)} {}
inline Value &operator=(Value const &v) {
v_ = v;
return v_;
}
inline Value &operator=(Value &&v) {
v_ = std::move(v);
return v_;
}
inline operator Value& () & {
return v_;
}
inline operator Value const & () const & {
return v_;
}
inline operator Value&& () && {
return std::move(v_);
}
public:
inline Value const &operator()() const { return v_; }
};
class S {
template <class T> using member_t = ROMember<S, T>;
public:
member_t<int> val = 0;
void f() { val = 1; }
};
int main() {
S s;
std::cout << s.val() << "\n";
s.f();
std::cout << s.val() << "\n";
return 0;
}
Some enable_ifs are missing to really be generic to the core, but the spirit is to make it re-usable and to keep the calls looking like getters.
This is indeed a trickery with friend.
You can use curiously recurring template pattern and friend the super class from within a property class like so:
#include <utility>
#include <cassert>
template<typename Super, typename T>
class property {
friend Super;
protected:
T& operator=(const T& val)
{ value = val; return value; }
T& operator=(T&& val)
{ value = val; return value; }
operator T && () &&
{ return std::move(value); }
public:
operator T const& () const&
{ return value; }
private:
T value;
};
struct wrap {
wrap() {
// Assign OK
prop1 = 5; // This is legal since we are friends
prop2 = 10;
prop3 = 15;
// Move OK
prop2 = std::move(prop1);
assert(prop1 == 5 && prop2 == 5);
// Swap OK
std::swap(prop2, prop3);
assert(prop2 == 15 && prop3 == 5);
}
property<wrap, int> prop1;
property<wrap, int> prop2;
property<wrap, int> prop3;
};
int foo() {
wrap w{};
w.prop1 = 5; // This is illegal since operator= is protected
return w.prop1; // But this is perfectly legal
}

How to pass argument in a singleton

I've been wondering how to pass argument to a singleton contructor. I already know how to do a singleton, but I've been unlucky to find a way to do it.
Here is my code (part of it).
Questionnary* Questionnary::getInstance(){
static Questionnary *questionnary = NULL;
if(questionnary == NULL){
cout << "Object created";
questionnary = new Questionnary();
}
else if(questionnary != NULL){
cout << "Object exist";
}
return questionnary;
}
Questionnary::Questionnary(){
cout << "I am an object";
}
//This is want i want to acheive
Questionnary::Questionnary(string name){
cout << "My name is << name;
}
Many thanks in advance
(BTW i know how and why a singleton is bad)
Let me extend Martin York's answer for your use case. I recommend using pointer for argument(s) in this particular situation, as we make use of its inherent property, that it can be "empty".
class Questionnary
{
std::string _str;
static Questionnary& getInstanceImpl(std::string* const s = nullptr)
{
static Questionnary instance{ s };
return instance;
}
Questionnary(std::string* const s)
: _str{ s ? move(*s) : std::string{} } // employ move ctor
{
if (nullptr == s)
throw std::runtime_error{ "Questionnary not initialized" };
}
public:
static Questionnary& getInstance()
{
return getInstanceImpl();
}
static void init(std::string s) // enable moving in
{
getInstanceImpl(&s);
}
Questionnary(Questionnary const&) = delete;
void operator=(Questionnary const&) = delete;
};
I find this approach less confusing, as it let's you get the instance after first initialization without (anyway discarded) arguments:
// first init
Questionnary::init("my single Questionnary");
// later on ...
Questionnary& q = Questionnary::getInstance();
Edit: Removed argument from getInstance function and a few optimizations.
You don't need to allocate the instance of singleton dynamically. It could look the following way (this is sometimes called "lazy loading singleton" ~ the instance is created late & kinda "automatically"):
#include <iostream>
#include <string>
class Questionnary
{
private:
// constructor taking string:
Questionnary(const std::string& name) : name_(name) { }
public:
static Questionnary& getInstance(const std::string& name)
{
static Questionnary q(name);
std::cout << "My name is: " << q.name_ << std::endl;
return q;
}
private:
std::string name_;
};
int main() {
Questionnary::getInstance("Josh");
Questionnary::getInstance("Harry");
}
output:
My name is: Josh
My name is: Josh
Note that constructor will be called only once right when the getInstance is called for the first time.
Have a method to create the instance to pass arguments to the constructor and you could assert in the getInstance() method if CreateInstance has not been called prior to calling it. Like:
class Questionnary
{
private:
// constructor taking string:
Questionnary(const std::string& name) : name_(name)
{
std::cout << "My name is: " << q.name_ << std::endl;
}
static Questionnary* m_instance;
public:
static void createInstance(const std::string& name)
{
assert(!m_instance);
m_instance = new Questionary(name);
}
static void destroyInstance()
{
assert(m_instance);
delete m_instance;
}
static Questionnary* Questionnary::getInstance()
{
assert(m_instance);
return m_instance;
}
private:
std::string name_;
};
//! #file singleton.h
//!
//! #brief Variadic template to make a singleton out of an ordinary type.
//!
//! This template makes a singleton out of a type without a default
//! constructor.
#ifndef SINGLETON_H
#define SINGLETON_H
#include <stdexcept>
template <typename C, typename ...Args>
class singleton
{
private:
singleton() = default;
static C* m_instance;
public:
singleton(const singleton&) = delete;
singleton& operator=(const singleton&) = delete;
singleton(singleton&&) = delete;
singleton& operator=(singleton&&) = delete;
~singleton()
{
delete m_instance;
m_instance = nullptr;
}
static C& create(Args...args)
{
if (m_instance != nullptr)
{
delete m_instance;
m_instance = nullptr;
}
m_instance = new C(args...);
return *m_instance;
}
static C& instance()
{
if (m_instance == nullptr)
throw std::logic_error(
"singleton<>::create(...) must precede singleton<>::instance()");
return *m_instance;
}
};
template <typename C, typename ...Args>
C* singleton<C, Args...>::m_instance = nullptr;
#endif // SINGLETON_H
and:
void
singleton_utest::test()
{
try
{
singleton<int, int>::instance();
UTEST_CHECK(false);
}
catch (std::logic_error& e)
{
UTEST_CHECK(true);
}
try
{
UTEST_CHECK((singleton<int, int>::create(1) == 1));
UTEST_CHECK((singleton<int, int>::instance() == 1));
}
catch (...)
{
UTEST_CHECK(false);
}
using stester0 = singleton<tester0>;
try
{
stester0::instance();
UTEST_CHECK(false);
}
catch (std::logic_error& e)
{
UTEST_CHECK(true);
}
try
{
UTEST_CHECK((stester0::create().result() == 0));
UTEST_CHECK((stester0::instance().result() == 0));
}
catch (...)
{
UTEST_CHECK(false);
}
using stester1 = singleton<tester1, int>;
try
{
stester1::instance();
UTEST_CHECK(false);
}
catch (std::logic_error& e)
{
UTEST_CHECK(true);
}
try
{
UTEST_CHECK((stester1::create(1).result() == 1));
UTEST_CHECK((stester1::instance().result() == 1));
}
catch (...)
{
UTEST_CHECK(false);
}
using stester2 = singleton<tester2, int, int>;
try
{
stester2::instance();
UTEST_CHECK(false);
}
catch (std::logic_error& e)
{
UTEST_CHECK(true);
}
try
{
UTEST_CHECK((stester2::create(1, 2).result() == 3));
UTEST_CHECK((stester2::instance().result() == 3));
}
catch (...)
{
UTEST_CHECK(false);
}
}
My version using Modern C++ that wraps an existing type:
#ifndef SINGLETON_H
#define SINGLETON_H
template <typename C, typename ...Args>
class singleton
{
private:
singleton() = default;
static C* m_instance;
public:
~singleton()
{
delete m_instance;
m_instance = nullptr;
}
static C& instance(Args...args)
{
if (m_instance == nullptr)
m_instance = new C(args...);
return *m_instance;
}
};
template <typename C, typename ...Args>
C* singleton<C, Args...>::m_instance = nullptr;
#endif // SINGLETON_H
Here is what in looks like in a unit test:
int &i = singleton<int, int>::instance(1);
UTEST_CHECK(i == 1);
tester1& t1 = singleton<tester1, int>::instance(1);
UTEST_CHECK(t1.result() == 1);
tester2& t2 = singleton<tester2, int, int>::instance(1, 2);
UTEST_CHECK(t2.result() == 3);
The problem with this is that instance() requires arguments on each call but only uses them on the first (as noted above). Default arguments cannot be used generally. It may be better to use a create(Args...) method which must precede the call of instance() or throw an exception.

Property like features in C++?

My use is pretty complicated. I have a bunch of objs and they are all passed around by ptr (not reference or value unless its an enum which is byval). At a specific point in time i like to call CheckMembers() which will check if each member has been set or is null. By default i cant make it all null because i wouldnt know if i set it to null or if it is still null bc i havent touch it since the ctor.
To assign a variable i still need the syntax to be the normal var = p; var->member = new Type;. I generate all the classes/members. So my question is how can i implement a property like feature where i can detect if the value has been set or left as the default?
I am thinking maybe i can use C++ with CLR/.NET http://msdn.microsoft.com/en-us/library/z974bes2.aspx but i never used it before and have no idea how well it will work and what might break in my C++ prj (it uses rtti, templates, etc).
Reality (edit): this proved to be tricky, but the following code should handle your requirements. It uses a simple counter in the base class. The counter is incremented once for every property you wish to track, and then decremented once for every property that is set. The checkMembers() function only has to verify that the counter is equal to zero. As a bonus, you could potentially report how many members were not initialized.
#include <iostream>
using namespace std;
class PropertyBase
{
public:
int * counter;
bool is_set;
};
template <typename T>
class Property : public PropertyBase
{
public:
T* ptr;
T* operator=(T* src)
{
ptr = src;
if (!is_set) { (*counter)--; is_set = true; }
return ptr;
}
T* operator->() { return ptr; }
~Property() { delete ptr; }
};
class Base
{
private:
int counter;
protected:
void TrackProperty(PropertyBase& p)
{
p.counter = &counter;
counter++;
}
public:
bool checkMembers() { return (counter == 0); }
};
class OtherObject : public Base { }; // just as an example
class MyObject : public Base
{
public:
Property<OtherObject> x;
Property<OtherObject> y;
MyObject();
};
MyObject::MyObject()
{
TrackProperty(x);
TrackProperty(y);
}
int main(int argc, char * argv[])
{
MyObject * object1 = new MyObject();
MyObject * object2 = new MyObject();
object1->x = new OtherObject();
object1->y = new OtherObject();
cout << object1->checkMembers() << endl; // true
cout << object2->checkMembers() << endl; // false
delete object1;
delete object2;
return 0;
}
There are a number of ways to do this, with varying tradeoffs in terms of space overhead. For example, here's one option:
#include <iostream>
template<typename T, typename OuterClass>
class Property
{
public:
typedef void (OuterClass::*setter)(const T &value);
typedef T &value_type;
typedef const T &const_type;
private:
setter set_;
T &ref_;
OuterClass *parent_;
public:
operator value_type() { return ref_; }
operator const_type() const { return ref_; }
Property<T, OuterClass> &operator=(const T &value)
{
(parent_->*set_)(value);
return *this;
}
Property(T &ref, OuterClass *parent, setter setfunc)
: set_(setfunc), ref_(ref), parent_(parent)
{ }
};
struct demo {
private:
int val_p;
void set_val(const int &newval) {
std::cout << "New value: " << newval << std::endl;
val_p = newval;
}
public:
Property<int, demo> val;
demo()
: val(val_p, this, &demo::set_val)
{ }
};
int main() {
demo d;
d.val = 42;
std::cout << "Value is: " << d.val << std::endl;
return 0;
}
It's possible to get less overhead (this has up to 4 * sizeof(void*) bytes overhead) using template accessors - here's another example:
#include <iostream>
template<typename T, typename ParentType, typename AccessTraits>
class Property
{
private:
ParentType *get_parent()
{
return (ParentType *)((char *)this - AccessTraits::get_offset());
}
public:
operator T &() { return AccessTraits::get(get_parent()); }
operator T() { return AccessTraits::get(get_parent()); }
operator const T &() { return AccessTraits::get(get_parent()); }
Property &operator =(const T &value) {
AccessTraits::set(get_parent(), value);
return *this;
}
};
#define DECL_PROPERTY(ClassName, ValueType, MemberName, TraitsName) \
struct MemberName##__Detail : public TraitsName { \
static ptrdiff_t get_offset() { return offsetof(ClassName, MemberName); }; \
}; \
Property<ValueType, ClassName, MemberName##__Detail> MemberName;
struct demo {
private:
int val_;
struct AccessTraits {
static int get(demo *parent) {
return parent->val_;
}
static void set(demo *parent, int newval) {
std::cout << "New value: " << newval << std::endl;
parent->val_ = newval;
}
};
public:
DECL_PROPERTY(demo, int, val, AccessTraits)
demo()
{ val_ = 0; }
};
int main() {
demo d;
d.val = 42;
std::cout << "Value is: " << (int)d.val << std::endl;
return 0;
}
This only consumes one byte for the property struct itself; however, it relies on unportable offsetof() behavior (you're not technically allowed to use it on non-POD structures). For a more portable approach, you could stash just the this pointer of the parent class in a member variable.
Note that both classes are just barely enough to demonstrate the technique - you'll want to overload operator* and operator->, etc, as well.
Here's my temporary alternative. One that doesn't ask for constructor parameters.
#include <iostream>
#include <cassert>
using namespace std;
template <class T>
class Property
{
bool isSet;
T v;
Property(Property&p) { }
public:
Property() { isSet=0; }
T operator=(T src) { v = src; isSet = 1; return v; }
operator T() const { assert(isSet); return v; }
bool is_set() { return isSet; }
};
class SomeType {};
enum SomeType2 { none, a, b};
class MyObject
{
public:
Property<SomeType*> x;
Property<SomeType2> y;
//This should be generated. //Consider generating ((T)x)->checkMembers() when type is a pointer
bool checkMembers() { return x.is_set() && y.is_set(); }
};
int main(int argc, char * argv[])
{
MyObject* p = new MyObject();
p->x = new SomeType;
cout << p->checkMembers() << endl; // false
p->y = a;
cout << p->checkMembers() << endl; // true
delete p->x;
delete p;
}