Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Comparison of Java and C++
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Templates vs. generics === Both C++ and Java provide facilities for [[generic programming]], [[Template (programming)|templates]] and [[Generics in Java|generics]], respectively. Although they were created to solve similar kinds of problems, and have similar syntax, they are quite different. :{| class="wikitable" ! C++ Templates ! Java Generics |- | Classes, functions, aliases<ref>{{cite web|website=cppreference.com| url=http://en.cppreference.com/w/cpp/language/type_alias| title=Type alias, alias template| accessdate=4 October 2022}}</ref> and variables<ref>{{cite web|url=http://en.cppreference.com/w/cpp/language/variable_template |website=cppreference.com |title=Variable template |accessdate=4 October 2022}}</ref> can be templated. | Classes and methods can be genericized. |- | Parameters can be variadic, of any type, integral value, character literal, or a class template. | Parameters can be any reference type, including boxed primitive types (i.e. Integer, Boolean...). |- | Separate instantiations of the class or function will be generated for each parameter-set when compiled. For class templates, only the member functions that are used will be instantiated. | One version of the class or function is compiled, works for all type parameters (via type-erasure). |- | Objects of a class template instantiated with different parameters will have different types at run time (i.e., distinct template instantiations are distinct classes). | Type parameters are erased when compiled; objects of a class with different type parameters are the same type at run time. It causes a different constructor. Because of this type erasure, it is not possible to overload methods using different instantiations of the generic class. |- | Implementation of the class or function template must be visible within a translation unit in order to use it. This usually implies having the definitions in the header files or included in the header file. As of [[C++11]], it is possible to use [[C++11#Extern template|extern templates]] to separate compiling of some instantiations. | Signature of the class or function from a compiled class file is sufficient to use it. |- | Templates can be [[Template (programming)#Explicit template specialization|specialized]]βa separate implementation could be provided for a particular template parameter. | Generics cannot be specialized. |- | Template parameters can have [[default argument]]s. Pre-[[C++11]], this was allowed only for template classes, not functions. | Generic type parameters cannot have default arguments. |- | Wildcards unsupported. Instead, return types are often available as nested [[typedef]]s. (Also, [[C++11]] added keyword <code>auto</code>, which acts as a wildcard for any type that can be determined at compile time.) | Wildcards supported as type parameter. |- | Bounding of type parameters and enforcement of relationships between type parameters effectively possible through metaprogramming,<ref>[http://www.boost.org/libs/type_traits/doc/html/boost_typetraits/reference.html Boost type traits library]</ref> or since C++20, directly via <code>std::derived_from</code> and other [[Concepts (C++)|concepts]] | Supports bounding of type parameters with "extends" and "super" for upper and lower bounds, respectively; allows enforcement of relationships between type parameters. |- | Allows instantiation of an object with the type of the parameter type. | Precludes instantiation of an object with the type of the parameter type (except via reflection). |- | Type parameter of class template can be used for static methods and variables. | Type parameter of generic class cannot be used for static methods and variables. |- | [[Static variable]]s unshared between classes and functions of different type parameters. | Static variables shared between instances of classes of different type parameters. |- | Class and function templates do not necessarily enforce type relations for type parameters in their declaration. Use of an incorrect type parameter results in compiling failure, often generating an error message within the template code rather than in the user's code that invokes it. Proper use of templated classes and functions is dependent on proper documentation. Metaprogramming provides these features at the cost of added effort. Since C++20, [[Concepts (C++)|concepts]] can be used to provide these features. | Generic classes and functions can enforce type relationships for type parameters in their declaration. Use of an incorrect type parameter results in a type error within the code that uses it. Operations on parametrized types in generic code are only allowed in ways that can be guaranteed to be safe by the declaration. This results in greater type safety at the cost of flexibility. |- | Templates are [[Turing-complete]] (see [[template metaprogramming]]). | Generics are also Turing-complete<ref>[https://arxiv.org/abs/1605.05274 Java Generics Are Turing Complete]</ref> |}
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Comparison of Java and C++
(section)
Add topic