Search code examples
c++templatesstlfunction-declaration

Standard way of shortening C++ function signatures


The templates always get really lengthy when I have certain vector and templated contained objects and the end just looks like a bunch of > > > > > > > that hardly helps discern boundaries sometimes, like this:

std::vector< std::pair< std::string, std::set< std::string > > >

Is there a standard way to reduce the length, or to make the items easily distinguishable? My actual code is this class declaration.

I have tried shortening the function name and typedef-ing the return value.

class Event_Maker
{
    public:

        virtual ~Event_Maker() {};

        // this one                                    *this juts out really far   v
        virtual std::vector< std::unique_ptr< Event > > transfer_events( void ) = 0;
};

Solution

  • Is there a standard way to reduce the length, or to make the items easily distinguishable?

    Type aliases using typedef can be handy, for example:

    typedef std::unique_ptr<Event> EventPtr;
    typedef std::vector<EventPtr> EventVector;
    virtual EventVector transfer_events( void ) = 0;
    

    Btw, you only need a space between >>, you don't need after < or before >:

    std::vector<std::pair<std::string, std::set<std::string> > >
    

    UPDATE

    As @Snowhawk04 and @mkrieger1 pointed out in comments, with modern compilers you don't need the space between >> anymore. Just make sure to use the right compiler flags, for example -std=c++0x with g++. See more info about >> and template aliases.