-1

I want a class which "has nothing" and you can't "do anything with it", but has something interesting happen when an instance of it is constructed. Now, sure, I can code that, e.g. something like

class MyIdiom final {
    MyIdiom() { /* magic goes here */ }

    /* maybe unnecessary? */
    MyIdiom(const MyIdiom&) = delete;
    operator=(const MyIdiom& other) = delete;
}

but I wonder if there isn't part of some commonly-used library, alongside other such "degenerate classes".

Note: Since people seem to be overly concerned with the use of such a class, suppose it's something like

template <typename F> class MyIdiom final {
    MyIdiom(F f) { f(); }
}

#define STATIC_BLOCK(_f) \
    auto MyIdiom<decltype(_f)> _myidiom_ # _f # _ # __LINE__ (_f);
einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

4

Provided that your object will only do something at construction and never be used for anything else, then you can pretty much just replace the whole class with a method (i.e. void) with the same parameters as the constructor. I'd call this the "unnecessary complication" idiom.

On the other hand I guess there may be some compile-time uses for it in situations like...

template<some things happening here>
MyIdiom() { }

so that the template magic takes place when that particular ctor is asked for. At the moment I can't think of anything other than setting flags when hacking stateful TMP to make this be useful.

  • About your first paragraph: You would be right if C++ had static blocks, see [this question](http://stackoverflow.com/q/19227664/1593077). – einpoklum Aug 09 '15 at 09:40