What is the meaning of "typedef sometype sometype"?

Recently, I have introduced the following construction into the code:

typedef sometype sometype; 

Note that “sometype” means exactly the same type without any additions such as “struct” etc.

I wonder why this might be useful?

UPD: This only works for custom types.

UPD2: the actual code was in the context of the template as follows:

 template <class T> struct E { typedef TT; ... } 
+6
c ++ typedef
source share
6 answers

How to make template parameters visible to external objects?

 template <class Foo> struct Bar { typedef Foo Foo; }; int main() { Bar<int>::Foo foo = 4; } 

Note. This is not actually allowed in standard C ++, but is specific to MSVC. See Comments.

+7
source share

Given your additional information about the templates, we can now respond.

A use case is when you want to specialize in a type of template. One typical example:

 template <typename T> struct nonconst { typedef T t; }; template <typename T> struct nonconst<T const> { typedef T t; }; 

This effectively removes the const qualifier from any type:

 nonconst<int>::tx; nonconst<int const>::ty; assert(typeid(x) == typeid(int)); assert(typeid(y) == typeid(int)); 

There are many similar use cases, for example. add (or remove) a pointer qualifier from a type, provide default and specialization values ​​for certain types, etc.

However, pay attention to the different shell of type names! Equal types in typedef TT are illegal C ++. [I stand fixed: §7.1.3.2] In addition, the de facto naming standard (cemented by its use in Boost libraries) is to invoke a type name with an alias type , for example:

 typedef T type; 
+7
source share

In C ++, you can put a typedef in a namespace or class, and then reference it relative to that namespace or class, which can be useful if the real type can change in the future.

eg.

 class IntHolder { public: typedef int int; IntHolder::int i; }; ... IntHolder foo; IntHolder::int i = foo.i; 

(NB: I did not check this correct syntax - but hopefully you get the idea)

If at some point you really need to hold long in IntHolder , you only need to change the IntHolder code.

Now, as a rule, you name a type differently, but maybe you can do it higher?

+6
source share

I have a theory. This may be the result of some refactoring. For example, a template type is not obscured by templates.

 typedef SomeCleverTemplate<Rocket> SuperThing; 

Then they removed the template, indicating that there was no other use in the code in it, and in order to be safe, they replaced each SomeCleverTemplate<Rocket> with SuperThing .

 typedef SuperThing SuperThing; 

Does it make sense in a real context?

+6
source share

As already mentioned, it works very well in a template:

 template <class Foo> struct Bar { typedef Foo Foo; }; 

But it can also be combined with template specialization:

 template <class Foo> struct Bar<Foo*> { typedef Foo Foo; }; 

Now I can do:

 Bar<int>::Foo i = 0; Bar<int*>::Foo j = i; 

Bar thus behaves effectively as a kind of shell type, which may be important for its interface (if there is bool equals(Foo i) const , for example).

Usually the selected name has some value_type value, for example ...

+1
source share

New idea! Some programming clubs, such as the widespread use of typedefs ...

zoo / animals / types.h

 namespace zoo { namespace animals { typedef size_t Count; // ... } // namespace animals } // namespace zoo 

zoo / animals / zebra.h

 #include "zoo/animals/types.h" namespace zoo { namespace animals { class Zebra { public: typedef Count Count; Count getLegCount() const; // ... }; // class Zebra } // namespace animals } // namespace zoo 

main.cpp

 #include "zoo/animals/zebra.h" int main() { typedef zoo::animals::Zebra Zebra; Zebra z; Zebra::Count n = z.getLegCount(); // Not as zoo::animals::Count // No using namespace zoo::animals required, // we are using just one item from there, the Zebra. // Definition of Zebra::Count may change, your usage remains robust. return 0; } 

I just have a similar situation at my workplace. Translating it may be a little stupid, but I would like to introduce it in a hurry.

0
source share

All Articles