S
Syron
Before I fell asleep last night, I had the following idea for in-class member initialization with no runtime overhead. What do you think?
#define INCLASS_INIT(ctype, name, ...) \
class _ ## name ## _INIT { \
private: ctype m_data; \
public: \
inline _ ## name ## _INIT() : \
m_data(__VA_ARGS__) {} \
inline _ ## name ## _INIT(const ctype& v) : \
m_data(v) {} \
inline operator ctype&() \
{ return m_data; } \
inline operator const ctype&() const \
{ return m_data; } \
inline ctype* operator&() \
{ return &m_data; } \
inline const ctype* operator&() const \
{ return &m_data; } \
inline ctype& operator=(const ctype& v) \
{ return m_data=v; } \
} name
// Usage:
class Foo {
public:
INCLASS_INIT(int, m_v1, 1);
INCLASS_INIT(int, m_v2, 2);
Foo() : m_v2(3)
{}
};
#define INCLASS_INIT(ctype, name, ...) \
class _ ## name ## _INIT { \
private: ctype m_data; \
public: \
inline _ ## name ## _INIT() : \
m_data(__VA_ARGS__) {} \
inline _ ## name ## _INIT(const ctype& v) : \
m_data(v) {} \
inline operator ctype&() \
{ return m_data; } \
inline operator const ctype&() const \
{ return m_data; } \
inline ctype* operator&() \
{ return &m_data; } \
inline const ctype* operator&() const \
{ return &m_data; } \
inline ctype& operator=(const ctype& v) \
{ return m_data=v; } \
} name
// Usage:
class Foo {
public:
INCLASS_INIT(int, m_v1, 1);
INCLASS_INIT(int, m_v2, 2);
Foo() : m_v2(3)
{}
};