C++ in-class member initialization

Discussion in 'C++' started by Syron, May 17, 2011.

  1. Syron

    Syron Guest

    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)
    {}
    };
    Syron, May 17, 2011
    #1
    1. Advertising

  2. On 5/17/2011 1:06 AM, Syron wrote:
    > 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)


    This is confusing. What's the value of m_v2? I see it initialized to
    '3' here, but is that what I'd see when I try using it elsewhere?

    > {}
    > };


    Curious (like a two-headed calf), but what's the point? Also, is it
    intentionally limited to simple types? For instance, you can't declare
    a reference that way, can you?

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 17, 2011
    #2
    1. Advertising

  3. Syron

    Krice Guest

    On 17 touko, 08:06, Syron <> wrote:
    > What do you think?


    I think it looks stupid and confusing. Well, better luck next time..
    Krice, May 18, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,711
    Thomas Weidenfeller
    Oct 12, 2005
  2. Siemel Naran
    Replies:
    4
    Views:
    786
    Micah Cowan
    Jan 12, 2005
  3. Replies:
    3
    Views:
    829
    Peter_Julian
    Oct 10, 2005
  4. Angus
    Replies:
    1
    Views:
    2,606
  5. aaragon
    Replies:
    2
    Views:
    605
    James Kanze
    Nov 2, 2008
Loading...

Share This Page