Talk:More C++ Idioms/Named Constructor

Factory method
Is this the same thing as the Factory Method pattern? --Fishpi (discuss • contribs) 20:57, 17 February 2011 (UTC)

I'm having severe doubts such a simple design guideline should be called an idiom at all. But let's assume it is. The implementation using static methods is "sub-optimal", as it returns by value which will entail one unnecessary copy construction. The obvious implementation for this "idiom" is by using type tags:

class Game { public: struct SinglePlayerTag {}; struct MultiPlayerTag {};

Game(SinglePlayerTag): Game(0) {} // named constructor Game(MultiPlayerTag ): Game(1) {} // named constructor private: Game (int game_type); };

An alternative is using Int2Type used on an enum specyfing single and multiplayer.