Question

I have some API with several data structures. In also contains some count of typedefs in different parts of API. I want to concentrate all of this typedefs in single special class with typedefs only, to simplify usage of my API, and treat them all in the same style all over API.

  1. Is there any conventional naming style for this kind of class containing typedefs only?
  2. Is it a good practice at all?
Était-ce utile?

La solution

Just use a C++ namespace. It allows you to group related items with a named scope without having to worry about using a class.

Autres conseils

No, it's not a good practice at all. Blindly gathering typedefs in one place makes absolutely no sense. Each typedef exists because of something and should be grouped with whatever it belongs to.

I think what you are referring to are the trait classes. They can be only used to provide additional information on specific types. If your typedefs are not type-related, they cannot be grouped around types, and trait classes are not the way to go.

Generally they look like

template<typename Type>
struct type_trait {
    typedef void type_trait; 
}

and are specialized for each type you are using:

template<>
struct type_trait<MyType>
{
    typedef TraitType type_trait; 
}

where TraitType is a sensible trait type (a characteristic type) of MyType, used in a generic algorithm (parametrized by Type):

typename type_trait<Type>::type_trait

The issue here for you is if the typedefs spread out over your API can be grouped around types. Struct is used and not a class to hold the typedefs, since they will be public anyway.

Here is a tutorial that you might find useful.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top