decltype

විකිපීඩියා, නිදහස් විශ්වකෝෂය වෙතින්
වෙත පනින්න: සංචලනය, සොයන්න

C++ ක්‍රමලේඛන භාෂාවෙහි, decltype යනු, ප්‍රකාශනයක වර්ගය විමසුම සඳහා වන මෙහෙයුම්කාරකයක් වෙයි .එය හඳුන්වාදෙන ලද්දේ, C++ සම්මතයෙහි වර්තමාන අනුවාදය වන්නාවූ, C++11 වෙතිනි. එහි ප්‍රයෝජනය වන්නේ සාමානය ක්‍රම ලේඛණයේ නිරන්තරයෙන් දුෂ්කර වන හා ඇතැම් විට සිදු කිරීමට නොහැකි වන, පරමිතික අච්චුව මත රදා පවතින ප්‍රකශණ වල වර්ගය සොයා ගැනීමයි.


සමානය ක්‍රම ලේඛණය 1990 සිට ජනප්‍රය වු අතර නිගමනය කීරීමේ ක්‍රියවලියක අවශයතාව හදුනා ගෙනුනි.බොහෝ සම්පාදක විකුණුම්කරුවන් type of ලෙස හදුන්වන මෙහෙයවන වල තමන්ගේම සංස්කරණ නිපදවු අතර පවතින භාෂා විශේෂාංග යොදා ඇතවුන් සීම සහිත වු ක්‍රියාකාරීත්වයක් ඇති නිපදවීම් ද කලහ.2002 දී Bjarne Stroustrup විසින් c++ භාෂාවට ප්‍රිමිතිකරණයක් අතුලත් කල යුතු යැයි යෝජනා කරන ලද අතර decl type ලෙස එහි නම යෝජනා කරන ලදී ප්‍රකාශණයක වර්ගය එමගින් උපත දෙනවා යන්න එහි තේරුමයි.


decltype's අර්ථවිචාරය නිරීක්ෂණය කරන ලද්දේ නවක වැඩසටහන් සම්පාදක වරුන්ගේ හා ලේඛණාල ලියන්නන්ගේ ඕනැකම් පිරිමසා ගැනීමටයි.පොදුවේ ගත් කල වස්තුවේ හෝ කර්යයක වර්ගය ප්‍රභව කේතයේ පවතින පරිදිම කරනු ලබයි.


Motivation[සංස්කරණය]

With the introduction of templates into the C++ programming language, and the advent of generic programming techniques pioneered by the Standard Template Library, the need for a mechanism for obtaining the type of an expression, commonly referred to as typeof, was recognized. In generic programming, it is often difficult or impossible to express types that depend on template parameters,[1][2] in particular the return type of function template instantiations.[1]

Many vendors provide the typeof operator as a compiler extension.[3] As early as 1997, before C++ was fully standardized, Brian Parker proposed a portable solution based on the sizeof operator.[3] His work was expanded on by Bill Gibbons, who concluded that the technique had several limitations and was generally less powerful than an actual typeof mechanism.[3] In an October 2000 article of Dr. Dobb's Journal, Andrei Alexandrescu remarked that "[h]aving a typeof would make much template code easier to write and understand."[4] He also noted that "typeof and sizeof share the same backend, because sizeof has to compute the type anyway."[4] Andrew Koenig and Barbara E. Moo also recognized the usefulness of a built-in typeof facility, with the caveat that "using it often invites subtle programming errors, and there are some problems that it cannot solve."[5] They characterized the use of type conventions, like the typedefs provided by the Standard Template Library, as a more powerful and general technique.[5] However, Steve Dewhurst argued that such conventions are "costly to design and promulgate", and that it would be "much easier to ... simply extract the type of the expression."[6] In a 2011 article on C++0x, Koenig and Moo predicted that "decltype will be widely used to make everyday programs easier to write."[7]

In 2002, Bjarne Stroustrup suggested extending the C++ language with mechanisms for querying the type of an expression, and initializing objects without specifying the type.[1] Stroustrup observed that the reference-dropping semantics offered by the typeof operator provided by the GCC and EDG compilers could be problematic.[1] Conversely, an operator returning a reference type based on the lvalue-ness of the expression was deemed too confusing. The initial proposal to the C++ standards committee outlined a combination of the two variants; the operator would return a reference type only if the declared type of the expression included a reference. To emphasize that the deduced type would reflect the "declared type" of the expression, the operator was proposed to be named decltype.[1]

One of the cited main motivations for the decltype proposal, was the ability to write perfect forwarding function templates.[8] It is sometimes desirable to write a generic forwarding function that returns the same type as the wrapped function, regardless of the type it is instantiated with. Without decltype, it is not generally possible to accomplish this.[8] An example, which also utilizes the trailing-return-type:[8]

int& foo(int& i);
float foo(float& f);
 
template <class T> auto transparent_forwarder(T& t)> decltype(foo(t)) {
  return foo(t);
}

decltype is essential here because it preserves the information about whether the wrapped function returns a reference type.[9]

Semantics[සංස්කරණය]

Similarly to the sizeof operator, the operand of decltype is unevaluated.[10] Informally, the type returned by decltype(e) is deduced as follows:[1]

  1. If the expression e refers to a variable in local or namespace scope, a static member variable or a function parameter, then the result is that variable's or parameter's declared type
  2. Otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e; if e is an xvalue, the result is T&&; otherwise, e is a prvalue and the result is T.

These semantics were designed to fulfill the needs of generic library writers, while at the same time being intuitive for novice programmers, because the return type of decltype always matches the type of the object or function exactly as declared in the source code.[1] More formally, Rule 1 applies to unparenthesized id-expressions and class member access expressions.[11] [12] Example:[11]

const int&& foo();
const int bar();
int i;
struct A { double x; };
const A* a = new A();
decltype(foo()) x1; // type is const int&&
decltype(bar()) x2; // type is int
decltype(i) x3; // type is int
decltype(a->x) x4; // type is double
decltype((a->x)) x5; // type is const double&

The reason for the difference between the latter two invocations of decltype is that the parenthesized expression (a->x) is neither an id-expression nor a member access expression, and therefore does not denote a named object.[13] Because the expression is an lvalue, its deduced type is "reference to the type of the expression", or const double&.[10]

In December 2008, a concern was raised to the committee by Jaakko Järvi over the inability to use decltype to form a qualified-id,[14] which is inconsistent with the intent that decltype(e) should be treated "as if it were a typedef-name".[15] While commenting on the formal Committee Draft for C++0x, the Japanese ISO member body noted that "a scope operator(::) cannot be applied to decltype, but it should be. It would be useful in the case to obtain member type(nested-type) from an instance as follows":[16]

vector<int> v;
decltype(v)::value_type i = 0; // int i = 0;

This, and similar issues pertaining to the wording inhibiting the use of decltype in the declaration of a derived class and in a destructor call, were addressed by David Vandevoorde, and voted into the working paper in March 2010.[17][18]

Availability[සංස්කරණය]

decltype is included in the current version of the C++ Language Standard, C++11.[11] It is provided by a number of compilers as an extension. Microsoft's Visual C++ 2010 compiler provides a decltype operator that closely mimics the semantics as described in the standards committee proposal. It can be used with both managed and native code.[9] The documentation states that it is "useful primarily to developers who write template libraries."[9] decltype was added to the mainline of the GCC C++ compiler in version 4.3,[19] released on March 5, 2008.[20] The operator is also present in Codegear's C++ Builder 2009,[21] the Intel C++ Compiler,[22] and Clang.[23]

References[සංස්කරණය]

  1. 1.0 1.1 1.2 1.3 1.4 1.5 1.6 Gregor, Douglas; Järvi, Jaakko; Siek, Jeremy; Stroustrup, Bjarne (2003-04-28). "Decltype and auto". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2009-08-13. 
  2. Kalev, Danny (2008-05-08). "Clean Up Function Syntax Mess with decltype". DevX.com. සම්ප්‍රවේශය 2009-09-04. 
  3. 3.0 3.1 3.2 Gibbons, Bill (2000-11-01). "A Portable "typeof" Operator". Dr. Dobb's Journal. සම්ප්‍රවේශය 2009-09-03. 
  4. 4.0 4.1 Alexandrescu, Andrei (2000-10-01). "Generic<Programming>: Mappings between Types and Values". Dr. Dobb's Journal. සම්ප්‍රවේශය 2009-09-03. 
  5. 5.0 5.1 Koenig, Andrew; Barbara E. Moo (2002-02-01). "C++ Made Easier: Naming Unknown Types". Dr. Dobb's Journal. සම්ප්‍රවේශය 2009-09-03. 
  6. Dewhurst, Steve (2000-08-01). "Common Knowledge: A Bitwise typeof Operator, Part 1". Dr. Dobb's Journal. සම්ප්‍රවේශය 2009-09-03. 
  7. Koenig, Andrew; Barbara E. Moo (2011-07-19). "4 Useful New Features in C++0x". Dr. Dobb's Journal. සම්ප්‍රවේශය 2012-01-12. 
  8. 8.0 8.1 8.2 Dos Reis, Gabriel; Järvi, Jaakko; Stroustrup, Bjarne (2004-10-12). "Decltype and auto (revision 4)". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2009-09-04. 
  9. 9.0 9.1 9.2 "decltype Operator". Microsoft Corporation. සම්ප්‍රවේශය 2009-09-04. 
  10. 10.0 10.1 Dos Reis, Gabriel; Järvi, Jaakko; Stroustrup, Bjarne (2007-07-18). "Decltype (revision 7): proposed wording". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2009-09-04. 
  11. 11.0 11.1 11.2 Becker, Pete. "Working Draft, Standard for Programming Language C++". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2009-09-04. 
  12. Miller, William M. (2009-08-03). "C++ Standard Core Language Defect Reports, Revision 65". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2009-09-15. 
  13. Miller, William M. (2009-08-03). "C++ Standard Core Language Closed Issues, Revision 65". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2009-09-04. 
  14. Miller, William M. (2009-09-29). "C++ Standard Core Language Active Issues, Revision 66". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2009-10-03. 
  15. Dos Reis, Gabriel; Järvi, Jaakko; Stroustrup, Bjarne (2006-11-05). "Decltype (revision 6): proposed wording". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2009-10-03. 
  16. Miller, William M. (2009-08-03). "C++ CD1 Comment Status". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2009-10-03. 
  17. Miller, William M. (2010-03-29). "C++ Standard Core Language Defect Reports, Revision 69". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2010-04-10. 
  18. Vandevoorde, Daveed (2010-02-03). "Core issues 743 and 950: Additional decltype(...) uses". ISO/IEC JTC1/SC22/WG21 – The C++ Standards Committee. සම්ප්‍රවේශය 2010-04-10. 
  19. "C++0x Support in GCC". Free Software Foundation. 2009-08-27. සම්ප්‍රවේශය 2009-09-04. 
  20. "GCC 4.3 Release Series". Free Software Foundation. 2009-08-13. සම්ප්‍රවේශය 2009-09-04. 
  21. "Type Specifier decltype (C++0x)". Embarcadero Technologies. සම්ප්‍රවේශය 2009-09-04. 
  22. "std, Qstd". Intel Corporation. සම්ප්‍රවේශය 2009-09-04. 
  23. Gregor, Douglas (2011-01-26). "New C++0x feature support in Clang". 

External links[සංස්කරණය]

"http://si.wikipedia.org/w/index.php?title=Decltype&oldid=295324" වෙතින් සම්ප්‍රවේශනය කෙරිණි