7 ms·
I like that using technique, but why is it using and not typedef? I thought using was meant for exposing names from superclasses.
by jamesaguilar 14y ago
I like that using technique, but why is it using and not typedef? I thought using was meant for exposing names from superclasses.
- yannickt 14y agoIn the OP example, "using" has the same semantics as "typedef". The new syntax emerged from the need for "template typedefs". http://www.stroustrup.com/C++11FAQ.html#template-alias http://www.stroustrup.com/C++11FAQ.html#template-alias
- jamesaguilar 14y agoOh. I wish they would have just shoehorned it into typedef somehow. I guess they must have had reasons why they couldn't.
- pmr_ 14y agoThe alias syntax has the benefit of not requiring typename everywhere because the syntax requires the right parts to be types and doesn't allow for ambiguities as does typedef. This makes code in dependent contexts a lot easier to read and to write.
- snogglethorpe 14y ago+1 on that; people intuitively expect template'd typedefs to work until they discover they don't. Oh well. You can read about the reasoning behind the new syntax here: http://www.emarcus.org/papers/n1449.pdf http://www.emarcus.org/papers/n1449.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1489.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n148...
- Sharlin 14y agoIn C++11 you can write "typedef templates". However, as "typedef" has always been a misleading keyword (it does not define a new type, it declares an alias for an existing type) it was decided to instead overload the using keyword: template <typename T> using MyVector = std::vector<T, MyAlloc>; As a side effect, it is now also possible to use using as a 1:1 replacement for typedef: // strictly equivalent to typedef int MyInt; using MyInt = int;
- AlexandrB 14y agoAfter reading the C++ FAQ on this I can accept that using tyepdef may have been impossible/confusing, but why overload "using"? This is one of the things that makes C++ mega-complicated. The same keywords have different meanings based on context, when really a new keyword should just have been used. For example "virtual" functions vs "virtual" inheritance.
- skrebbel 14y agoThe moment you add a keyword to a popular language, you break lots of existing code that happen to have that keyword as an identifier, e.g. as a variable, class or function. This means that when adding features that need a new keyword, you best overload an existing one. It's for this reason, too, that Java 7 overloaded `try` to implement some sort of poor-man's-RAII-support, instead of adding a new keyword (such as C#'s `using`, which does the same, and was by the way also overloaded for the same reason)