Internal typedefs in C++ - good style or bad style?

前端 未结 9 1443
天涯浪人
天涯浪人 2020-12-07 07:28

Something I have found myself doing often lately is declaring typedefs relevant to a particular class inside that class, i.e.

class Lorem
{
    typedef boost         


        
9条回答
  •  感动是毒
    2020-12-07 07:40

    It serves as a statement of intent - in the example above, the Lorem class is intended to be reference counted via boost::shared_ptr and stored in a vector.

    This is exactly what it does not do.

    If I see 'Foo::Ptr' in the code, I have absolutely no idea whether it's a shared_ptr or a Foo* (STL has ::pointer typedefs that are T*, remember) or whatever. Esp. if it's a shared pointer, I don't provide a typedef at all, but keep the shared_ptr use explicitly in the code.

    Actually, I hardly ever use typedefs outside Template Metaprogramming.

    The STL does this type of thing all the time

    The STL design with concepts defined in terms of member functions and nested typedefs is a historical cul-de-sac, modern template libraries use free functions and traits classes (cf. Boost.Graph), because these do not exclude built-in types from modelling the concept and because it makes adapting types that were not designed with the given template libraries' concepts in mind easier.

    Don't use the STL as a reason to make the same mistakes.

提交回复
热议问题