CINXE.COM
Co je nov茅ho v C++11 | Petr Zemek | 膶esk媒 blog
<!DOCTYPE html> <html lang="cs" dir="ltr" style="--color--primary-hue:202;--color--primary-saturation:79%;--color--primary-lightness:50"> <head> <meta charset="utf-8" /> <meta name="Generator" content="Drupal 10 (https://www.drupal.org)" /> <meta name="MobileOptimized" content="width" /> <meta name="HandheldFriendly" content="true" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="icon" href="/images/favicon.ico" type="image/vnd.microsoft.icon" /> <link rel="canonical" href="https://cs-blog.petrzemek.net/2012-12-04-co-je-noveho-v-cpp11" /> <link rel="shortlink" href="https://cs-blog.petrzemek.net/node/116" /> <title>Co je nov茅ho v C++11 | Petr Zemek | 膶esk媒 blog</title> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/align.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/fieldgroup.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/container-inline.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/clearfix.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/details.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/hidden.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/item-list.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/js.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/nowrap.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/position-container.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/reset-appearance.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/resize.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/system-status-counter.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/system-status-report-counters.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/system-status-report-general-info.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/modules/system/css/components/tablesort.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/modules/geshifilter/assets/css/geshifilter.css?sncyar" /> <link rel="stylesheet" media="all" href="/modules/captcha/css/captcha.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/base/fonts.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/base/variables.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/base/base.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/layout.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/grid.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/layout-content-narrow.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/layout-content-medium.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/layout-footer.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/region.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/region-content.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/region-hero.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/region-secondary-menu.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/social-bar.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/layout/views.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/block.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/breadcrumb.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/embedded-media.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/footer.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/button.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/container-inline.module.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/fieldset.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/field.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/form.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/form-boolean.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/form-text.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/form-textarea.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/form-select.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/header-buttons-mobile.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/header-navigation.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/header-site-branding.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/header-sticky-toggle.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/hero.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/links.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/messages.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/navigation/nav-button-mobile.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/node.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/node-teaser.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/page-title.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/site-header.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/skip-link.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/pager.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/table.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/text-content.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/wide-content.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/comments.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/navigation/nav-primary.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/navigation/nav-primary-button.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/navigation/nav-primary-wide.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/header-search-wide.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/theme/filter.theme.css?sncyar" /> <link rel="stylesheet" media="all" href="/core/themes/olivero/css/components/feed.css?sncyar" /> <link rel="stylesheet" media="all" href="/sites/default/files/asset_injector/css/lower_spacing-a350a976c0c721b12bbb4f11a95a9955.css?sncyar" /> <link rel="stylesheet" media="all" href="/sites/default/files/asset_injector/css/remove_underlines_from_links-480e4baf451a722c96ce52e85877c1bd.css?sncyar" /> <link rel="stylesheet" media="all" href="/sites/default/files/asset_injector/css/show_tags_inline-654d65b3d14075be3fb856fe736d5ca1.css?sncyar" /> <link rel="preload" href="/core/themes/olivero/fonts/metropolis/Metropolis-Regular.woff2" as="font" type="font/woff2" crossorigin> <link rel="preload" href="/core/themes/olivero/fonts/metropolis/Metropolis-SemiBold.woff2" as="font" type="font/woff2" crossorigin> <link rel="preload" href="/core/themes/olivero/fonts/metropolis/Metropolis-Bold.woff2" as="font" type="font/woff2" crossorigin> <link rel="preload" href="/core/themes/olivero/fonts/lora/lora-v14-latin-regular.woff2" as="font" type="font/woff2" crossorigin> <noscript><link rel="stylesheet" href="/core/themes/olivero/css/components/navigation/nav-primary-no-js.css?sncyar" /> </noscript> </head> <body class="path-node page-node-type-blog"> <a href="#main-content" class="visually-hidden focusable skip-link"> P艡ej铆t k hlavn铆mu obsahu </a> <div class="dialog-off-canvas-main-canvas" data-off-canvas-main-canvas> <div id="page-wrapper" class="page-wrapper"> <div id="page"> <header id="header" class="site-header" data-drupal-selector="site-header" role="banner"> <div class="site-header__fixable" data-drupal-selector="site-header-fixable"> <div class="site-header__initial"> <button class="sticky-header-toggle" data-drupal-selector="sticky-header-toggle" role="switch" aria-controls="site-header__inner" aria-label="Sticky header" aria-checked="false"> <span class="sticky-header-toggle__icon"> <span></span> <span></span> <span></span> </span> </button> </div> <div id="site-header__inner" class="site-header__inner" data-drupal-selector="site-header-inner"> <div class="container site-header__inner__container"> <div id="block-olivero-site-branding" class="site-branding--bg-white site-branding block block-system block-system-branding-block"> <div class="site-branding__inner"> <a href="/" rel="home" class="site-branding__logo"> <img src="/images/logo-me.png" alt="Dom暖" fetchpriority="high" /> </a> <div class="site-branding__text"> <div class="site-branding__name"> <a href="/" title="Dom暖" rel="home">Petr Zemek | 膶esk媒 blog</a> </div> </div> </div> </div> <div class="header-nav-overlay" data-drupal-selector="header-nav-overlay"></div> <div class="mobile-buttons" data-drupal-selector="mobile-buttons"> <button class="mobile-nav-button" data-drupal-selector="mobile-nav-button" aria-label="Main Menu" aria-controls="header-nav" aria-expanded="false"> <span class="mobile-nav-button__label">Menu</span> <span class="mobile-nav-button__icon"></span> </button> </div> <div id="header-nav" class="header-nav" data-drupal-selector="header-nav"> <nav id="block-olivero-main-menu" class="primary-nav block block-menu navigation menu--main" aria-labelledby="block-olivero-main-menu-menu" role="navigation"> <h2 class="visually-hidden block__title" id="block-olivero-main-menu-menu">Hlavn铆 navigace</h2> <ul class="menu primary-nav__menu primary-nav__menu--level-1" data-drupal-selector="primary-nav-menu--level-1"> <li class="primary-nav__menu-item primary-nav__menu-item--link primary-nav__menu-item--level-1"> <a href="/" class="primary-nav__menu-link primary-nav__menu-link--link primary-nav__menu-link--level-1" data-drupal-selector="primary-nav-menu-link-has-children" data-drupal-link-system-path="<front>"> <span class="primary-nav__menu-link-inner primary-nav__menu-link-inner--level-1">Dom暖</span> </a> </li> <li class="primary-nav__menu-item primary-nav__menu-item--link primary-nav__menu-item--level-1"> <a href="https://petrzemek.net/" title="P艡ej铆t na moji osobn铆 str谩nku" class="primary-nav__menu-link primary-nav__menu-link--link primary-nav__menu-link--level-1" data-drupal-selector="primary-nav-menu-link-has-children"> <span class="primary-nav__menu-link-inner primary-nav__menu-link-inner--level-1">Osobn铆 str谩nka</span> </a> </li> <li class="primary-nav__menu-item primary-nav__menu-item--link primary-nav__menu-item--level-1"> <a href="https://blog.petrzemek.net/" title="P艡ej铆t na m暖j anglick媒 blog" class="primary-nav__menu-link primary-nav__menu-link--link primary-nav__menu-link--level-1" data-drupal-selector="primary-nav-menu-link-has-children"> <span class="primary-nav__menu-link-inner primary-nav__menu-link-inner--level-1">Anglick媒 blog</span> </a> </li> </ul> </nav> <div class="region region--secondary-menu"> <div class="search-block-form block block-search-wide" data-drupal-selector="search-block-form" id="block-olivero-search-form-wide" role="search"> <button class="block-search-wide__button" aria-label="Search Form" data-drupal-selector="block-search-wide-button"> <svg xmlns="http://www.w3.org/2000/svg" width="22" height="23" viewBox="0 0 22 23"> <path fill="currentColor" d="M21.7,21.3l-4.4-4.4C19,15.1,20,12.7,20,10c0-5.5-4.5-10-10-10S0,4.5,0,10s4.5,10,10,10c2.1,0,4.1-0.7,5.8-1.8l4.5,4.5c0.4,0.4,1,0.4,1.4,0S22.1,21.7,21.7,21.3z M10,18c-4.4,0-8-3.6-8-8s3.6-8,8-8s8,3.6,8,8S14.4,18,10,18z"/> </svg> <span class="block-search-wide__button-close"></span> </button> <div class="block-search-wide__wrapper" data-drupal-selector="block-search-wide-wrapper" tabindex="-1"> <div class="block-search-wide__container"> <div class="block-search-wide__grid"> <form action="/search/node" method="get" id="search-block-form" accept-charset="UTF-8" class="search-form search-block-form"> <div class="js-form-item form-item js-form-type-search form-item-keys js-form-item-keys form-no-label"> <label for="edit-keys" class="form-item__label visually-hidden">Hledat</label> <input title="Zadejte hledan媒 term铆n." placeholder="Vyhledat slovo 膷i fr谩zi." data-drupal-selector="edit-keys" type="search" id="edit-keys" name="keys" value="" size="15" maxlength="128" class="form-search form-element form-element--type-search form-element--api-search" /> </div> <div data-drupal-selector="edit-actions" class="form-actions js-form-wrapper form-wrapper" id="edit-actions"><button class="button--primary search-form__submit button js-form-submit form-submit" data-drupal-selector="edit-submit" type="submit" id="edit-submit" value="Hledat"> <span class="icon--search"></span> <span class="visually-hidden">Hledat</span> </button> </div> </form> </div> </div> </div> </div> </div> </div> </div> </div> </div> </header> <div id="main-wrapper" class="layout-main-wrapper layout-container"> <div id="main" class="layout-main"> <div class="main-content"> <a id="main-content" tabindex="-1"></a> <div class="main-content__container container"> <div class="region region--highlighted grid-full layout--pass--content-medium"> <div data-drupal-messages-fallback class="hidden messages-list"></div> </div> <div class="region region--breadcrumb grid-full layout--pass--content-medium"> <div id="block-olivero-breadcrumbs" class="block block-system block-system-breadcrumb-block"> <div class="block__content"> <nav class="breadcrumb" role="navigation" aria-labelledby="system-breadcrumb"> <h2 id="system-breadcrumb" class="visually-hidden">Drobe膷kov谩 navigace</h2> <div class="breadcrumb__content"> <ol class="breadcrumb__list"> <li class="breadcrumb__item"> <a href="/" class="breadcrumb__link">Dom暖</a> </li> </ol> </div> </nav> </div> </div> </div> <main role="main"> <div class="region region--content-above grid-full layout--pass--content-medium"> <div id="block-olivero-page-title" class="block block-core block-page-title-block"> <h1 class="title page-title"> <span>Co je nov茅ho v C++11</span> </h1> </div> </div> <div class="region region--content grid-full layout--pass--content-medium" id="content"> <div id="block-garland-system-main" class="block block-system block-system-main-block"> <div class="block__content"> <article data-history-node-id="116" class="node node--type-blog node--promoted node--view-mode-full"> <header class=""> <div class="node__meta"> <span> Od <span><span>Petr Zemek</span></span>, 2012-12-04 </span> </div> </header> <div class="node__content"> <div class="text-content clearfix field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>N臎kte艡铆 ji啪 mo啪n谩 zaregistrovali, 啪e v sprnu lo艌sk茅ho roku byl schv谩len nov媒 standard jazyka <a href="http://en.wikipedia.org/wiki/C%2B%2B">C++</a>: <a href="http://en.wikipedia.org/wiki/C%2B%2B11">ISO C++11</a>. Ten byl b臎hem sv茅 p艡铆pravy zn谩m pod n谩zvem C++0x. O tom, co je v n臎m nov茅ho oproti p艡edchoz铆mu standardu, C++98, se dozv铆te v tomto <strong>velmi</strong> dlouh茅m p艡铆sp臎vku :).</p> <!--break--><p><strong>Pozn谩mka na za膷谩tek</strong>: Tento p艡铆sp臎vek koresponduje k m茅 <a href="http://talks.petrzemek.net/Petr-Zemek_-_2012-12-04_-_What.Is.New.In.Cpp11.pdf">prezentaci</a> na intern铆m semin谩艡i projektu <a href="http://www.fit.vutbr.cz/research/groups/lissom/team.html">Lissom</a> (sekce rekonfigurovateln茅ho zp臎tn茅ho p艡eklada膷e).</p> <h2>O 膷em si budeme pov铆dat?</h2> <p>Nejd艡铆ve si stru膷n臎 projdeme historii jazyk暖 C a C++. Pot茅 se pod铆v谩me na to, co je v C++11 nov茅ho oproti C++98, a钮 u啪 z hlediska jazyka samotn茅ho tak z hlediska standardn铆 knihovny. Jeliko啪 je zm臎n velmi mnoho a v拧echny je neprojdeme, tak bude n谩sledovat seznam v拧eho, k 膷emu jsme se nedostali. Od C++11 pak n臎kter茅 z谩le啪itosti z C++98 zastaraly 膷i byly odebr谩ny - dozv铆te se, kter茅 膷谩sti to jsou. No a jeliko啪 nen铆 nad to si v拧e p臎kn臎 vyzkou拧et, tak se na z谩v臎r dozv铆te, jak je to s podporou nov茅 normy v p艡eklada膷铆ch, standardn铆ch knihovn谩ch a ostatn铆ch n谩stroj铆ch, kter茅 pracuj铆 se zdrojov媒mi soubory (<a href="http://www.stack.nl/~dimitri/doxygen/">doxygen</a>).</p> <p>C铆lem je, abyste nov茅 vlastnosti dostali do sv茅ho podv臎dom铆. Rozhodn臎 si nekladu za c铆l d暖kladn茅 sezn谩men铆 se v拧铆m, co v nov茅 norm臎 je. Sp铆拧e se zam臎艡铆m na v臎ci, kter茅 m臎 zaujaly. V mnoha p艡铆padech budu v媒klad sm臎艡ovat od p艡铆kladu v C++98 a uk谩z谩n铆, jak se to d谩 ud臎lat v C++11.</p> <p>Jdeme na to :). Za膷neme histori铆.</p> <h2>Stru膷n谩 historie jazyk暖 C a C++</h2> <p>Jeliko啪 jsou jazyky C a C++ velmi prov谩zan茅, projdeme si historii obou, a to p臎kn臎 chronologicky, aby v tom byl po艡谩dek.</p> <ul> <li><strong>1973 - C</strong> Prvn铆 verze jazyka C, kterou vytvo艡il <a href="http://en.wikipedia.org/wiki/Dennis_Ritchie">Dennis Ritchie</a>. Na jeho v媒voji se b臎hem let 1969 a啪 1973 pod铆leli i dal拧铆 zn谩m铆 lid茅, nap艡. <a href="http://en.wikipedia.org/wiki/Brian_Kernighan">Brian Kernighan</a> a <a href="http://en.wikipedia.org/wiki/Ken_Thompson_(computer_programmer)">Ken Thompson</a>.</li> <li><strong>1978 - K&R C</strong> P艡ed t铆m, ne啪 byl jazyk C v暖bec standardizov谩n, tak bylo za "standard" pova啪ov谩no tzv. "C podle Kernighana a Ritchieho", kter茅 bylo pops谩no v prvn铆m vyd谩n铆 knihy <a href="http://en.wikipedia.org/wiki/The_C_Programming_Language_(book)">The C Programming Language</a>. Mezi vlastnosti, kterou v t茅 dob臎 jazyk C m臎l, pat艡il z dne拧n铆ho pohledu zvl谩拧tn铆 zp暖sob z谩pisu funkc铆, kde typy parametr暖 byly specifikov谩ny a啪 za uzav铆rac铆 z谩vorkou ")" a implicitn铆m typem byl <span class="geshifilter"><code class="text geshifilter-text">int</code></span>.</li> <li><strong>1981 - C with Classes</strong> <a href="http://en.wikipedia.org/wiki/Bjarne_Stroustrup">Bjarne Stroustrup</a> zah谩jil od roku 1979 pr谩ci na roz拧铆艡en铆 jazyka C o t艡铆dy a dal mu n谩zev "C s t艡铆dami". P艡eklad tohoto nov茅ho jazyka prob铆hal tak, 啪e se zdrojov茅 k贸dy nejd艡铆ve p艡evedly do jazyka C a n谩sledn臎 se pou啪il C茅膷kov媒 p艡eklada膷.</li> <li><strong>1985 - C++</strong> V roce 1983 do拧lo ke zm臎n臎 n谩zvu jazyka na C++, co啪 m谩 b媒t h艡铆膷ka vyu啪铆vaj铆c铆 oper谩tor inkrementace a m谩 zna膷it n谩sledn铆ka jazyka C. V roce 1985 pak vy拧la kniha <a href="http://en.wikipedia.org/wiki/The_C%2B%2B_Programming_Language">The C++ Programming Language</a>, kter谩, obdobn臎 jako The C Programming Language u jazyka C, slou啪ila jaho tehdej拧铆 jazykov谩 reference.</li> <li><strong>1990 - ISO C90</strong> Jazyk C byl standardizov谩n mezin谩rodn铆 organizac铆 ISO. N臎kte艡铆 mo啪n谩 v铆, 啪e v roce 1989 byl jazyk C standardizov谩n organizac铆 ANSI prov谩d臎j铆c铆 standardizaci pro Severn铆 Ameriku. Tomuto standardu se 艡铆k谩 ANSI C89. ISO tento standard p艡evzalo, pouze provedlo drobn茅 zm臎ny, p艡edev拧铆m t媒kaj铆c铆 se form谩tov谩n铆. Proto se normy ISO C90 a ANSI C89 daj铆 pova啪ovat za toto啪n茅. Standardizace pomohla roz拧铆艡en铆 tohoto jazyka.</li> <li><strong>1998 - ISO C++98</strong> Prvn铆 standard jazyka C++.</li> <li><strong>1999 - ISO C99</strong> Druh媒 standard jazyka C. Mezi nov茅 vlastnosti pat艡铆 nap艡. inlining funkc铆, typ <span class="geshifilter"><code class="text geshifilter-text">long long</code></span>, komplexn铆 膷铆sla, pole na z谩sobn铆ku, jeho啪 d茅lka je ur膷ena a啪 za b臎hu programu, a zaveden铆 艡谩dkov茅ho koment谩艡e (<span class="geshifilter"><code class="text geshifilter-text">//</code></span>). Jako zaj铆mavost lze uv茅st, 啪e standardy C99 a C++98 vznikaly pom臎rn臎 odd臎len臎 a obsahuj铆 mnoho odli拧nost铆 (nap艡. v C++ chyb臎l a啪 do standardu C++11 typ <span class="geshifilter"><code class="text geshifilter-text">long long int</code></span>).</li> <li><strong>2003 - ISO C++03</strong> V roce 2003 prob臎hla na z谩klad臎 tzv. "defect report暖" oprava standardu ISO C++98. Jeliko啪 se v拧ak jednalo o detaily 膷i se zm臎ny t媒kaly jen tv暖rc暖 p艡eklada膷暖, tak se tato oprava nijak nerozli拧uje od vlastn铆ho standardu C++98.</li> <li><strong>2007 - ISO C++TR1</strong> V roce 2005 vznikl dokument <a href="http://en.wikipedia.org/wiki/Technical_Report_1">Technical Report 1 (TR1)</a>, kter媒 obsahuje roz拧铆艡en铆 standardu C++98 o vlastnosti p艡ipravovan茅 pro nov媒 standard C++ 膷i o vlastnosti p艡ejat茅 z C99. Jak p艡铆klad lze zm铆nit chytr茅 ukazatele (angl. smart pointers), ntice a hashovac铆 tabulky. Nejedn谩 se o standard jako takov媒, sp铆拧e o dokument obsahuj铆c铆 n谩vrh nov媒ch vlastnost铆. Jeho 煤膷elem bylo, aby se tv暖rci standardn铆ch knihoven a p艡eklada膷暖 p艡ipravili na n谩stup nov茅ho standardu a sjednotili rozhran铆 k roz拧铆艡en铆m oproti C++98. Mnoho p艡eklada膷暖 toti啪 nab铆zelo nap艡. hashovac铆 tabulky je拧t臎 p艡ed t铆mto dokumentem. Pokud dan媒 p艡eklada膷 poskytoval funkcionalitu z tohoto dokumentu, pak ji um铆stil do jmenn茅ho prostoru <span class="geshifilter"><code class="text geshifilter-text">std::tr1</code></span>.</li> <li><strong>2011 - ISO C++11</strong> Nejnov臎j拧铆 standard jazyka C++. O n臎m bude 艡e膷 ve zbytku p艡铆sp臎vku i v p艡铆padn媒ch dal拧铆ch p艡铆sp臎vc铆ch :).</li> <li><strong>2011 - ISO C11</strong> Jazyk C se v minul茅m roce takt茅啪 do膷kal revize, kter谩 ov拧em, alespo艌 z m茅ho pohledu, nen铆 tak velk谩, jako u C++11. K novink谩m v t茅to norm臎 se mo啪n谩 dostanu v n臎kter茅m z budouc铆ch p艡铆sp臎vk暖.</li> </ul> <h2>Co je nov茅ho</h2> <p>Pot茅, co jsem v谩s ur膷it臎 dokonale unudil histori铆, za膷neme s pr暖zkumem nov茅ho standardu. Nejd艡铆ve pod铆v谩me na nov茅 jazykov茅 vlastnosti a pot茅 na novinky ve standardn铆 knihovn臎.</p> <h3><strong>Automatick谩 typov谩 inference (auto)</strong></h3> <p>V C++98 je t艡eba u ka啪d茅 prom臎nn茅 specifikovat jej铆 typ, i kdy啪 je z kontextu jasn茅, o jak媒 typ jde. Pokud jste nap艡. cht臎li iterovat p艡es vektor 膷铆sel, mohli jste pou啪铆t n谩sleduj铆c铆 <span class="geshifilter"><code class="text geshifilter-text">for</code></span> cyklus:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">for</span> <span style="color: #008000;">(</span>vector<span style="color: #000080;"><</span><span style="color: #0000ff;">int</span><span style="color: #000080;">></span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> i <span style="color: #000080;">=</span> v.<span style="color: #007788;">begin</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span>, e <span style="color: #000080;">=</span> v.<span style="color: #007788;">end</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> i <span style="color: #000040;">!</span><span style="color: #000080;">=</span> e<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> process<span style="color: #008000;">(</span><span style="color: #000040;">*</span>i<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>Pozn谩mka bokem: V拧imn臎te si ulo啪en铆 <span class="geshifilter"><code class="text geshifilter-text">v.end()</code></span> do prom臎nn茅 <span class="geshifilter"><code class="text geshifilter-text">e</code></span> v inicializa膷n铆 膷谩st铆 cyklu, aby se tato metoda nemusela volat p艡i ka啪d茅m vol谩n铆 cyklu. Tohle je u啪ite膷n媒 idiom pro iteraci nad kontejnery.</p> <p>Z kontextu je jasn茅, co bude typem prom臎nn媒ch <span class="geshifilter"><code class="text geshifilter-text">i</code></span> a <span class="geshifilter"><code class="text geshifilter-text">e</code></span>. C++11 umo啪艌uje se t茅to explicitn铆 specifikaci typu vyhnout za pou啪it铆 kl铆膷ov茅ho slova <span class="geshifilter"><code class="text geshifilter-text">auto</code></span>:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">for</span> <span style="color: #008000;">(</span><span style="color: #0000ff;">auto</span> i <span style="color: #000080;">=</span> v.<span style="color: #007788;">begin</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span>, e <span style="color: #000080;">=</span> v.<span style="color: #007788;">end</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> i <span style="color: #000040;">!</span><span style="color: #000080;">=</span> e<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> process<span style="color: #008000;">(</span><span style="color: #000040;">*</span>i<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>Jako dal拧铆 p艡铆klad lze zm铆nit vyhled谩n铆 prvku v <span class="geshifilter"><code class="text geshifilter-text">std::map<></code></span>:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;"><</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span>, <span style="color: #0000ff;">int</span><span style="color: #000080;">></span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> pos <span style="color: #000080;">=</span> m.<span style="color: #007788;">find</span><span style="color: #008000;">(</span>target<span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>V C++11 sta膷铆 napsat:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">auto</span> pos <span style="color: #000080;">=</span> m.<span style="color: #007788;">find</span><span style="color: #008000;">(</span>target<span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>co啪 je mnohem p艡ehledn臎j拧铆.</p> <p>Jako pozn谩mku na z谩v臎r bych zm铆nil, 啪e kl铆膷ov茅 slovo <span class="geshifilter"><code class="text geshifilter-text">auto</code></span> je v jazyc铆ch C a C++ u啪 od jejich po膷谩tku, ov拧em mimo C++11 m谩 <a href="/?q=node/37">odli拧n媒 v媒znam</a>: jedn谩 se o specifik谩tor pam臎钮ov茅 t艡铆dy pro lok谩ln铆 prom臎nn茅. Jeliko啪 je v拧ak tato pam臎钮ov谩 t艡铆da u lok谩ln铆ch prom臎nn媒ch implicitn铆, je jeho pou啪it铆 zbyte膷n茅. Pokud by v谩s zaj铆malo vyu啪it铆 tohoto kl铆膷ov茅ho slova ve star拧铆ch verz铆ch jazyka C, mrkn臎te <a href="http://stackoverflow.com/questions/1046477/is-there-any-reason-to-use-the-auto-keyword-in-c-c">zde</a>.</p> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1984.pdf">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Type_inference">1</a>, <a href="http://akrzemi1.wordpress.com/2012/03/27/gotchas-of-type-inference/">2</a>, <a href="http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html">3</a>, <a href="http://lanzkron.wordpress.com/2011/02/21/inferring-too-much">4</a>, <a href="http://geekswithblogs.net/BlackRabbitCoder/archive/2012/03/15/c-little-wonders-the-c11-auto-keyword-redux.aspx">5</a></p> <h3><strong>Nov谩 verze cyklu for</strong></h3> <p>Uva啪ujme je拧t臎 jednou onen cyklus z p艡edch谩zej铆c铆 膷谩sti o typov茅 inferenci:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">for</span> <span style="color: #008000;">(</span>vector<span style="color: #000080;"><</span><span style="color: #0000ff;">int</span><span style="color: #000080;">></span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> i <span style="color: #000080;">=</span> v.<span style="color: #007788;">begin</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span>, e <span style="color: #000080;">=</span> v.<span style="color: #007788;">end</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> i <span style="color: #000040;">!</span><span style="color: #000080;">=</span> e<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> process<span style="color: #008000;">(</span><span style="color: #000040;">*</span>i<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>C++11 nab铆z铆 dal拧铆 typ <span class="geshifilter"><code class="text geshifilter-text">for</code></span> cyklu, zn谩m媒 v jin媒ch jazyc铆ch pod n谩zvem <a href="http://en.wikipedia.org/wiki/Foreach_loop">foreach cyklus</a>. Pomoc铆 n臎ho lze velmi snadno iterovat nad v拧emi prvky dan茅ho kontejneru:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++1</span> <span style="color: #0000ff;">for</span> <span style="color: #008000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&</span> x <span style="color: #008080;">:</span> v<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> process<span style="color: #008000;">(</span>x<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>Sta膷铆, aby dan媒 kontejner poskytoval metody <span class="geshifilter"><code class="text geshifilter-text">begin()</code></span> a <span class="geshifilter"><code class="text geshifilter-text">end()</code></span>.</p> <p>Mezi dal拧铆 v媒hody tohoto typu cyklu pat艡铆 to, 啪e pomoc铆 n臎ho lze iterovat i p艡es klasick谩 C茅膷kov谩 pole:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> numbers<span style="color: #008000;">[</span><span style="color: #008000;">]</span> <span style="color: #000080;">=</span> <span style="color: #008000;">{</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">5</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">for</span> <span style="color: #008000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&</span> x <span style="color: #008080;">:</span> numbers<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> x <span style="color: #000040;">*</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>V拧imn臎te si pou啪it铆 reference (pokud bychom referenci nepou啪ili, tak by se hodnoty prvk暖 v poli nezm臎nily). V C++98 bychom museli iteraci prov茅st takto:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">for</span> <span style="color: #008000;">(</span><span style="color: #0000ff;">size_t</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>, e <span style="color: #000080;">=</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">(</span>numbers<span style="color: #008000;">)</span><span style="color: #000040;">/</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">(</span>numbers<span style="color: #008000;">[</span><span style="color: #0000dd;">0</span><span style="color: #008000;">]</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> i <span style="color: #000040;">!</span><span style="color: #000080;">=</span> e<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> numbers<span style="color: #008000;">[</span>i<span style="color: #008000;">]</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>Z谩v臎re膷n茅 pozn谩mky:</p> <ul> <li>M铆sto explicitn铆 specifikace typu lze vyu啪铆t kl铆膷ov茅 slovo <span class="geshifilter"><code class="text geshifilter-text">auto</code></span>. Onen typ prom臎nn茅 by pak v obou p艡铆padech byl <span class="geshifilter"><code class="text geshifilter-text">int &</code></span>.</li> <li>Pokud chcete prov茅st iteraci v opa膷n茅m po艡ad铆 (tzv. reverse iteration), tak je pot艡eba <a href="http://stackoverflow.com/questions/8542591/c11-reverse-range-based-for-loop">tro拧ku v铆c k贸du</a>.</li> </ul> <p>Odkazy: <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2930.html">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Range-based_for_loop">1</a></p> <h3><strong>Inicializace kontejner暖 v媒膷tem prvk暖</strong></h3> <p>Klasick茅 C茅膷kov茅 pole lze nainicializovat v媒膷tem prvk暖 snadno:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">int</span> a<span style="color: #008000;">[</span><span style="color: #008000;">]</span> <span style="color: #000080;">=</span> <span style="color: #008000;">{</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">5</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>Obt铆啪n臎j拧铆 to je ji啪 u kontejner暖 ze standardn铆 knihovny, kdy pokud bychom cht臎li vytvo艡it vektor obsahuj铆c铆 stejn谩 膷铆sla, jako pole v媒拧e, museli bychom ud臎lat n臎co takov茅hoto:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span><span style="color: #0000ff;">int</span><span style="color: #000080;">></span> v<span style="color: #008080;">;</span> v.<span style="color: #007788;">push_back</span><span style="color: #008000;">(</span><span style="color: #0000dd;">1</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ...</span> v.<span style="color: #007788;">push_back</span><span style="color: #008000;">(</span><span style="color: #0000dd;">5</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>Samoz艡ejm臎, existuj铆 i jin茅 varianty, ale v拧echny zahrnuj铆 pot艡ebu naps谩n铆 k贸du nav铆c. V C++11 je situace mnohem snadn臎j拧铆. Skute膷n臎, inicializaci vektoru lze prov茅st takto:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span><span style="color: #0000ff;">int</span><span style="color: #000080;">></span> v <span style="color: #000080;">=</span> <span style="color: #008000;">{</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">5</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>Ani to nebolelo a m谩me nainicializovan媒 vektor :). Takov媒to zp暖sob inicializace vektoru lze vyu啪铆t i v p艡铆pad臎, 啪e vol谩te funkci, kter谩 o膷ek谩v谩 vektor:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">void</span> f<span style="color: #008000;">(</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000080;">></span> v<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #666666;">// ...</span> <span style="color: #008000;">}</span> f<span style="color: #008000;">(</span><span style="color: #008000;">{</span><span style="color: #FF0000;">"a"</span>, <span style="color: #FF0000;">"b"</span>, <span style="color: #FF0000;">"c"</span><span style="color: #008000;">}</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>Nejlep拧铆 na tom je, 啪e takto to funguje i u ostatn铆ch kontejner暖 ze standardn铆 knihovny, nikoliv jen u vektoru. D谩le, podporu pro inicializaci t铆mto zp暖sobem <a href="http://en.cppreference.com/w/cpp/utility/initializer_list">lze doimplementovat i do va拧ich t艡铆d</a>.</p> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2672.htm">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Initializer_lists">1</a>, <a href="http://en.cppreference.com/w/cpp/utility/initializer_list">2</a></p> <h3><strong>Jednotn谩 inicializace objekt暖</strong></h3> <p>M臎jme n谩sleduj铆c铆 kus k贸du:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> BasicStruct <span style="color: #008000;">{</span> <span style="color: #0000ff;">int</span> x<span style="color: #008080;">;</span> <span style="color: #0000ff;">double</span> y<span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">struct</span> ClassStruct <span style="color: #008000;">{</span> ClassStruct<span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #008000;">{</span><span style="color: #008000;">}</span> ClassStruct<span style="color: #008000;">(</span><span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">double</span> y<span style="color: #008000;">)</span><span style="color: #008080;">:</span> x<span style="color: #008000;">(</span>x<span style="color: #008000;">)</span>, y<span style="color: #008000;">(</span>y<span style="color: #008000;">)</span> <span style="color: #008000;">{</span><span style="color: #008000;">}</span> <span style="color: #0000ff;">int</span> x<span style="color: #008080;">;</span> <span style="color: #0000ff;">double</span> y<span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>Je v n臎m definovan谩 klasick谩 C茅膷kov谩 struktura a pot茅 struktura s konstruktory, kter谩 se chov谩 jako t艡铆da. Pokud bychom cht臎li vytvo艡it instanci <span class="geshifilter"><code class="text geshifilter-text">BasicStruct</code></span> a nainicializovat ji, m暖啪eme pou啪铆t n谩sleduj铆c铆 syntaxi, kter谩 poch谩z铆 ji啪 z dob jazyka C:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> BasicStruct a <span style="color: #000080;">=</span> <span style="color: #008000;">{</span><span style="color: #0000dd;">1</span>, <span style="color:#800080;">2.5</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>P艡i inicializaci <span class="geshifilter"><code class="text geshifilter-text">ClassStruct</code></span> mus铆me pou啪铆t n谩sleduj铆c铆 syntaxi (nejedn谩 se ji啪 toti啪 o klasickou strukturu, ale o t艡铆du):</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> ClassStruct b<span style="color: #008000;">(</span><span style="color: #0000dd;">2</span>, <span style="color:#800080;">8.9</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>No a pokud bychom cht臎li inicializovat <span class="geshifilter"><code class="text geshifilter-text">ClassStruct</code></span> pomoc铆 jin茅ho objektu t茅 stejn茅 t艡铆dy, mus铆me napsat:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> ClassStruct c<span style="color: #008000;">(</span><span style="color: #008000;">(</span>ClassStruct<span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ?</span></pre></div> <p>Zd谩nliv茅 z谩vorky nav铆c jsou nutn茅. Z谩pis bez nich by toti啪 znamenal deklaraci funkce <span class="geshifilter"><code class="text geshifilter-text">c</code></span>, kter谩 vrac铆 <span class="geshifilter"><code class="text geshifilter-text">ClassStruct</code></span> a bere jako parametr ukazatel na funkci bez parametr暖 vracej铆c铆 <span class="geshifilter"><code class="text geshifilter-text">ClassStruct</code></span>... Stoj铆 za t铆m <a href="http://en.wikipedia.org/wiki/Most_vexing_parse">zp暖sob rozli拧en铆 nejednozna膷n媒ch konstrukc铆 v C++</a>.</p> <p>V拧imn臎te si, 啪e ve v拧ech t艡ech p艡铆padech jsme museli pou啪铆t jinou syntaxi. Jednou to byly slo啪en茅 z谩vorky, jednou klasick茅, a ve t艡et铆m p艡铆pad臎 dvojit茅 klasick茅 z谩vorky. C++11 toto 艡e拧铆 zaveden铆m jednotn茅ho zp暖sobu inicializace:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> BasicStruct a<span style="color: #008000;">{</span><span style="color: #0000dd;">1</span>, <span style="color:#800080;">2.5</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span> ClassStruct b<span style="color: #008000;">{</span><span style="color: #0000dd;">2</span>, <span style="color:#800080;">8.9</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span> ClassStruct c<span style="color: #008000;">{</span>ClassStruct<span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>Vypad谩 to zvl谩拧tn臎, 啪e? :) V臎艡te, 啪e v C++ se budete se slo啪en媒mi z谩vorkami setk谩vat mnohem v铆ce, ne啪 obvykle. Nap艡铆klad t艡eba u anonymn铆ch funkc铆ch, na kter茅 se mrkneme za chv铆li.</p> <p>Odkazy: <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Uniform_initialization">1</a></p> <h3><strong>Alternativn铆 z谩pis funkc铆 a kl铆膷ov茅 slovo decltype</strong></h3> <p>C++11 zav谩d铆 alternativn铆 z谩pis funkc铆. M铆sto</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">int</span> f<span style="color: #008000;">(</span><span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>lze nyn铆 ps谩t</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">auto</span> f<span style="color: #008000;">(</span><span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">)</span> <span style="color: #000040;">-</span><span style="color: #000080;">></span> <span style="color: #0000ff;">int</span><span style="color: #008080;">;</span></pre></div> <p>Te膹 si asi 艡铆k谩te n臎co ve smyslu: "Proboha, pro膷?" Tento nov媒 z谩pis se hod铆 v n谩sleduj铆c铆ch dvou situac铆ch:</p> <ul> <li><strong>Vyu啪it铆 v 拧ablon谩ch</strong>. Uva啪ujte n谩sleduj铆c铆 拧ablonu funkce, kter谩 se膷te dv臎 p艡edan茅 hodnoty a vr谩t铆 v媒sledek:<br> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">template</span><span style="color: #000080;"><</span><span style="color: #0000ff;">class</span> Lhs, <span style="color: #0000ff;">class</span> Rhs<span style="color: #000080;">></span> <span style="color: #0000ff;">auto</span> add<span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> Lhs<span style="color: #000040;">&</span> lhs, <span style="color: #0000ff;">const</span> Rhs<span style="color: #000040;">&</span> rhs<span style="color: #008000;">)</span> <span style="color: #000040;">-</span><span style="color: #000080;">></span> <span style="color: #0000ff;">decltype</span><span style="color: #008000;">(</span>lhs <span style="color: #000040;">+</span> rhs<span style="color: #008000;">)</span> <span style="color: #008000;">{</span><span style="color: #0000ff;">return</span> lhs <span style="color: #000040;">+</span> rhs<span style="color: #008080;">;</span><span style="color: #008000;">}</span></pre></div> <p> Nov媒 oper谩tor <span class="geshifilter"><code class="text geshifilter-text">decltype</code></span> vr谩t铆 typ p艡edan茅ho v媒razu. Pokud bychom u t茅to 拧ablony pou啪ili klasick媒 styl z谩pisu funkce, pak by p艡eklad selhal. D暖vod je ten, 啪e na m铆st臎, kde je te膹 kl铆膷ov茅 slovo <span class="geshifilter"><code class="text geshifilter-text">auto</code></span>, nem暖啪eme napsat <span class="geshifilter"><code class="text geshifilter-text">decltype(lhs + rhs)</code></span>, proto啪e v t茅 dob臎 je拧t臎 p艡eklada膷 nev铆, co to je <span class="geshifilter"><code class="text geshifilter-text">lhs</code></span> a <span class="geshifilter"><code class="text geshifilter-text">rhs</code></span>.</p></li> <li><strong>Odstran臎n铆 opakov谩n铆</strong>. Uva啪ujme n谩sleduj铆c铆 t艡铆du, kter谩 m谩 dlouh茅 jm茅no:<br> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> LongClassName <span style="color: #008000;">{</span> <span style="color: #0000ff;">typedef</span> std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span><span style="color: #0000ff;">int</span><span style="color: #000080;">></span> IntVec<span style="color: #008080;">;</span> IntVec f<span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p> V n铆 je pou啪it <span class="geshifilter"><code class="text geshifilter-text">typedef</code></span> pro vektor 膷铆sel a deklarov谩na funkce <span class="geshifilter"><code class="text geshifilter-text">f()</code></span>. Nyn铆, kdy啪 bychom m臎li definovat funkci <span class="geshifilter"><code class="text geshifilter-text">f()</code></span>, tak v C++98 bychom museli napsat:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> LongClassName<span style="color: #008080;">::</span><span style="color: #007788;">IntVec</span> LongClassName<span style="color: #008080;">::</span><span style="color: #007788;">f</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #ff0000; font-style: italic;">/* ... */</span> <span style="color: #008000;">}</span></pre></div> <p> V拧imn臎te si dvojn谩sobn茅 kvalifikace <span class="geshifilter"><code class="text geshifilter-text">LongClassName</code></span>, kter谩 je nutn谩 (p艡eklada膷 p艡ed t铆m, ne啪 naraz铆 na <span class="geshifilter"><code class="text geshifilter-text">LongClassName::f()</code></span>, nev铆, 啪e se budeme pohybovat v prostoru jmen dan茅 t艡铆dy). S vyu啪it铆m alternativn铆ho z谩pisu lze prvn铆 kvalifikaci vynechat:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">auto</span> LongClassName<span style="color: #008080;">::</span><span style="color: #007788;">f</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #000040;">-</span><span style="color: #000080;">></span> IntVec <span style="color: #008000;">{</span> <span style="color: #ff0000; font-style: italic;">/* ... */</span> <span style="color: #008000;">}</span></pre></div> </li> </ul> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2541.htm">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Alternative_function_syntax">1</a>, <a href="http://oopscenities.net/2011/05/04/c0x-decltype/">2</a></p> <h3><strong>Anonymn铆 (lambda) funkce</strong></h3> <p><a href="http://en.wikipedia.org/wiki/Anonymous_function">Anonymn铆, alias nepojmenovan茅 膷i lambda funkce</a>, pat艡铆 mezi sou膷谩st mnoha vysoko煤rov艌ov媒ch jazyk暖. S novou normou p艡ich谩z铆 i jejich podpora do C++.</p> <p>Za膷neme p艡铆kladem. Anonymn铆 funkci, kter谩 n谩m se膷te dv臎 膷铆sla, m暖啪eme zapsat n谩sledovn臎.</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #008000;">[</span><span style="color: #008000;">]</span><span style="color: #008000;">(</span><span style="color: #0000ff;">int</span> x, <span style="color: #0000ff;">int</span> y<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">return</span> x <span style="color: #000040;">+</span> y<span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>Hranat茅 z谩vorky n谩sledovan茅 kulat媒mi n谩m ur膷uj铆, 啪e se bude jednat o lambda funkci. Funkce m谩 dva parametry typu <span class="geshifilter"><code class="text geshifilter-text">int</code></span> a vrac铆 jejich sou膷et. Takovou funkci m暖啪eme vyu啪铆t na m铆stech, kde se o膷ek谩v谩 funkce 膷i <a href="http://en.wikipedia.org/wiki/Function_object">functor</a> (funk膷n铆 objekt).</p> <p>Obecn谩 syntaxe je n谩sleduj铆c铆:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #008000;">[</span>capture<span style="color: #008000;">]</span><span style="color: #008000;">(</span>parameters<span style="color: #008000;">)</span> <span style="color: #000040;">-</span><span style="color: #000080;">></span> return<span style="color: #000040;">-</span>type <span style="color: #008000;">{</span> body <span style="color: #008000;">}</span></pre></div> <p>Obsah hranat媒ch z谩vorek n谩m ur膷uje, jak se budou do funkce p艡ed谩vat prom臎nn茅 z okol铆 (viz d谩le). Parametry jsou pak zaps谩ny klasicky, jak jsme zvykl铆. N谩sleduje specifikace n谩vratov茅ho typu, kter谩 se zapisuje on铆m nov媒m stylem, o kter茅m jsme si pov铆dali p艡ed chv铆l铆. Pokud m谩 t臎lo funkce jen jedin媒 p艡铆kaz, kter媒 vrac铆 hodnotu, pak se n谩vratov媒 typ detekuje automaticky a nen铆 t艡eba jej explicitn臎 zmi艌ovat. N谩sleduje t臎lo funkce ve slo啪en媒ch z谩vork谩ch.</p> <p>Poj膹me si uk谩zat, jak se to d谩 pou啪铆t. Dejme tomu, 啪e m谩me t艡铆du <span class="geshifilter"><code class="text geshifilter-text">Person</code></span>, kter谩 obsahuje informace o osob臎. D谩le m臎jme vektor osob, kter媒 chceme se艡adit podle jejich identifik谩toru (ID, unik谩tn铆 膷铆slo v r谩mci organizace). K tomu vyu啪ijeme standardn铆 funkci <span class="geshifilter"><code class="text geshifilter-text">std::sort()</code></span>, kter茅 p艡ed谩me rozsah, kter媒 budeme se艡azovat a funkci, kter谩 se pou啪ije pro 艡azen铆:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> std<span style="color: #008080;">::</span><span style="color: #007788;">sort</span><span style="color: #008000;">(</span>people.<span style="color: #007788;">begin</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span>, people.<span style="color: #007788;">end</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span>, <span style="color: #008000;">[</span><span style="color: #008000;">]</span><span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> Person<span style="color: #000040;">&</span> p1, <span style="color: #0000ff;">const</span> Person<span style="color: #000040;">&</span> p2<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">return</span> p1.<span style="color: #007788;">getId</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #000080;"><</span> p2.<span style="color: #007788;">getId</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>Jak si m暖啪ete v拧imnout, tak jsme s v媒hodou vyu啪ili mo啪nosti, kterou n谩m poskytuje C++11 a jeho lambda funkce. V opa膷n茅m p艡铆pad臎 bychom si museli tuto 艡ad铆c铆 funkci vytvo艡it mimo tento k贸d, co啪 by jej mohlo znep艡ehlednit.</p> <p>V dal拧铆 uk谩zce se dozv铆me, k 膷emu slou啪铆 obsah hranat媒ch z谩vorek. M臎jme vektor 膷铆sel a ozna膷me si jej <span class="geshifilter"><code class="text geshifilter-text">v</code></span>. Na拧铆m 煤kolem je se膷铆st v拧echny jeho hodnoty a ulo啪it je do prom臎nn茅 <span class="geshifilter"><code class="text geshifilter-text">total</code></span>. Ud臎l谩me to nap艡. takto:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span><span style="color: #0000ff;">int</span><span style="color: #000080;">></span> v <span style="color: #000080;">=</span> <span style="color: #666666;">// ...</span> <span style="color: #0000ff;">int</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">for_each</span><span style="color: #008000;">(</span>v.<span style="color: #007788;">begin</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span>, v.<span style="color: #007788;">end</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span>, <span style="color: #008000;">[</span><span style="color: #000040;">&</span>total<span style="color: #008000;">]</span><span style="color: #008000;">(</span><span style="color: #0000ff;">int</span> x<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> x<span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>Samoz艡ejm臎 by to 拧lo 艡e拧it i jin媒m zp暖sobem - toto je jen uk谩zka. Op臎t jsme vyu啪ili standardn铆 funkce, tentokr谩t <span class="geshifilter"><code class="text geshifilter-text">std::for_each()</code></span>. V拧imn臎te si, 啪e do hranat媒ch z谩vorek jsme museli napsat <span class="geshifilter"><code class="text geshifilter-text">&total</code></span>, 膷铆m啪 si zaru膷铆me, 啪e se prom臎nn谩 <span class="geshifilter"><code class="text geshifilter-text">total</code></span> p艡ed谩 do funkce referenc铆, nikoliv hodnotou, jak by tomu bylo, kdybychom nechali tyto z谩vorky pr谩zdn茅. T铆mto zp暖sobem se daj铆 v C++ implementovat a pou啪铆t tzv. <a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29">closures</a>.</p> <p>Odkazy: <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2927.pdf">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Lambda_functions_and_expressions">1</a>, <a href="http://www.cprogramming.com/c++11/c++11-lambda-closures.html">2</a></p> <h3><strong>Konstanta nullptr pro nulov媒 ukazatel</strong></h3> <p>V C++98 se jako nulov媒 ukazatel d谩 pou啪铆t bu膹 konstanta 0, nebo C茅膷kov茅 makro <span class="geshifilter"><code class="text geshifilter-text">NULL</code></span> (je v拧ak doporu膷ov谩no pou啪铆vat 0). Ani jeden z p艡铆stup暖 v拧ak nen铆 ide谩ln铆. M臎jme n谩sleduj铆c铆 dv臎 funkce:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> f<span style="color: #008000;">(</span><span style="color: #0000ff;">int</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">void</span> f<span style="color: #008000;">(</span><span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>Kdy啪 se pokus铆me zavolat <span class="geshifilter"><code class="text geshifilter-text">f()</code></span> s nulov媒m ukazatelem, tak dopadneme v obou p艡铆padech stejn臎:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> f<span style="color: #008000;">(</span><span style="color: #0000dd;">0</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// zavol谩 void f(int);</span> f<span style="color: #008000;">(</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// takt茅啪 zavol谩 void f(int);</span></pre></div> <p>膶lov臎k by o膷ek谩val, 啪e <span class="geshifilter"><code class="text geshifilter-text">f(NULL)</code></span> zavol谩 <span class="geshifilter"><code class="text geshifilter-text">void f(int *)</code></span>, ale nen铆 tomu tak. D暖vod je ten, 啪e v C++ je makro <span class="geshifilter"><code class="text geshifilter-text">NULL</code></span> definov谩no jako celo膷铆seln谩 nula.</p> <p>C++11 p艡ich谩z铆 s 艡e拧en铆m, kter媒m je nov茅 kl铆膷ov茅 slovo <span class="geshifilter"><code class="text geshifilter-text">nullptr</code></span>, co啪 je nulov媒 ukazatel. Je typu <span class="geshifilter"><code class="text geshifilter-text">std::nullptr_t</code></span> a je implicitn臎 konvertovateln媒 na jak媒koliv jin媒 ukazatelov媒 typ a na <span class="geshifilter"><code class="text geshifilter-text">bool</code></span>, co啪 se hod铆 nap艡. p艡i testech, zda ukazatel nen铆 nulov媒. Pro konverzi na <span class="geshifilter"><code class="text geshifilter-text">int</code></span> je t艡eba pou啪铆t <span class="geshifilter"><code class="text geshifilter-text">reinterpret_cast<></code></span>. Pou啪it铆 je n谩sleduj铆c铆:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>pc <span style="color: #000080;">=</span> <span style="color: #0000ff;">nullptr</span><span style="color: #008080;">;</span> <span style="color: #666666;">// OK</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span>pi <span style="color: #000080;">=</span> <span style="color: #0000ff;">nullptr</span><span style="color: #008080;">;</span> <span style="color: #666666;">// OK</span> <span style="color: #0000ff;">bool</span> b <span style="color: #000080;">=</span> <span style="color: #0000ff;">nullptr</span><span style="color: #008080;">;</span> <span style="color: #666666;">// OK (b je false)</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000ff;">nullptr</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ! (chyba p艡i p艡ekladu)</span> f<span style="color: #008000;">(</span><span style="color: #0000dd;">0</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// zavol谩 void f(int);</span> f<span style="color: #008000;">(</span><span style="color: #0000ff;">nullptr</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// zavol谩 void f(int *);</span></pre></div> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Null_pointer_constant">1</a></p> <h3><strong>Siln臎 typovan茅 v媒膷ty</strong></h3> <p>V媒膷ty se v C++98 chovaj铆 jinak, ne啪 t艡铆dy. Uva啪ujme n谩sleduj铆c铆 k贸d:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">enum</span> MyEnum <span style="color: #008000;">{</span> VAL1, VAL2, VAL3 <span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> VAL2<span style="color: #008080;">;</span></pre></div> <p>Za prv茅, v C++98 je v媒膷et skrz naskrz kompatibiln铆 s <span class="geshifilter"><code class="text geshifilter-text">int</code></span>em, tak啪e je lze mezi sebou libovoln臎 p艡ev谩d臎t. Lze mezi sebou p艡ev谩d臎t a porovn谩vat i hodnoty r暖zn媒ch v媒膷t暖. Za druh茅, <span class="geshifilter"><code class="text geshifilter-text">enum</code></span> nevytv谩艡铆 prostor jmen, tak啪e v拧echny jeho prvky jsou sou膷谩st铆 jmenn茅ho prostoru, ve kter茅m je definov谩n onen <span class="geshifilter"><code class="text geshifilter-text">enum</code></span>. To zp暖sobuje probl茅my, pokud pot艡ebujete v铆ce r暖zn媒ch v媒膷t暖 se stejn媒mi konstantami. Za t艡et铆, standard nedefinuje, co p艡esn臎 za typ se m谩 pou啪铆t pro ulo啪en铆 v媒膷tu, tak啪e to m暖啪e b媒t <span class="geshifilter"><code class="text geshifilter-text">char</code></span>, <span class="geshifilter"><code class="text geshifilter-text">int</code></span>, 膷i cokoliv jin茅ho. Zn谩t typ, kter媒 se pou啪il k ulo啪en铆, je v媒hodn茅 v situaci, pokud chcete nap艡. prvky onoho <span class="geshifilter"><code class="text geshifilter-text">enum</code></span>u serializovat a pos铆lat p艡es s铆钮.</p> <p>C++11 tyto probl茅my adresuje zaveden铆m tzv. siln臎 typovan媒ch v媒膷t暖:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">enum</span> <span style="color: #0000ff;">class</span> MyEnum<span style="color: #008080;">:</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> <span style="color: #008000;">{</span> VAL1, VAL2, VAL3, <span style="color: #666666;">// :)</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> MyEnum<span style="color: #008080;">::</span><span style="color: #007788;">VAL2</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ! (chyba p艡i p艡ekladu)</span></pre></div> <p>V拧imn臎te si pou啪it铆 kl铆膷ov茅ho slova <span class="geshifilter"><code class="text geshifilter-text">class</code></span>. U t臎chto v媒膷t暖 voliteln臎 specifikovat typ, na kter茅m budou ulo啪eny (implicitn臎 <span class="geshifilter"><code class="text geshifilter-text">int</code></span>, zde <span class="geshifilter"><code class="text geshifilter-text">unsigned int</code></span>). D谩le, tvo艡铆 ji啪 samostatn媒 prostor jmen, tak啪e na stejn茅 煤rovni m暖啪e existovat v铆ce v媒膷t暖 se stejn臎 pojmenovan媒mi konstantami. D暖le啪it媒 fakt tak茅 je, 啪e prvky v媒膷t暖 ji啪 nejsou implicitn臎 p艡evediteln茅 na <span class="geshifilter"><code class="text geshifilter-text">int</code></span> ani na jak媒koliv jin媒 v媒膷et. Pot臎拧铆 i to, 啪e nyn铆 膷谩rka za posledn铆m prvkem v <span class="geshifilter"><code class="text geshifilter-text">enum</code></span>u ji啪 nen铆 syntaktick谩 chyba :).</p> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Strongly_typed_enumerations">1</a>, <a href="http://nic-gamedev.blogspot.cz/2012/04/c11-strongly-typed-enum.html">2</a></p> <h3><strong>Explicitn铆 redefinice</strong></h3> <p>V C++98 m暖啪ete narazit na zaj铆mav媒 a z谩ke艡n媒 probl茅m, pokud se pokus铆te redefinovat (angl. override) metodu, ale zm臎n铆te jej铆 signaturu:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">class</span> Base <span style="color: #008000;">{</span> <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> f<span style="color: #008000;">(</span><span style="color: #0000ff;">int</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">class</span> Derived<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Base <span style="color: #008000;">{</span> <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> f<span style="color: #008000;">(</span><span style="color: #0000ff;">double</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// skryje Base::f()</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>Ve v媒拧e uveden茅m k贸du je ten probl茅m, 啪e m铆sto redefinice metody <span class="geshifilter"><code class="text geshifilter-text">f()</code></span> z nadt艡铆dy dojde k jej铆mu p艡ekryt铆 novou metodou, kter谩 m谩 parametr typu double. To v谩m pozd臎ji m暖啪e zp暖sobit bolesti hlavy p艡i lad臎n铆. Sice si m暖啪ete 艡铆ct, 啪e v谩m se to nikdy nestane, ale co kdy啪 se majitel b谩zov茅 t艡铆dy rozhodne, 啪e zm臎n铆 signaturu n臎kter茅 z virtu谩ln铆ch metod a vy si toho nev拧imnete?</p> <p>V C++11 se tomu d谩 vyhnout uveden铆m identifik谩toru <span class="geshifilter"><code class="text geshifilter-text">override</code></span> za hlavi膷ku on茅 metody, u kter茅 si chceme b媒t jisti, 啪e n谩m redefinuje ji啪 existuj铆c铆 metodu v nadt艡铆d臎:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">class</span> Base <span style="color: #008000;">{</span> <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> f<span style="color: #008000;">(</span><span style="color: #0000ff;">int</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">class</span> Derived<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Base <span style="color: #008000;">{</span> <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> f<span style="color: #008000;">(</span><span style="color: #0000ff;">double</span><span style="color: #008000;">)</span> <span style="color: #0000ff;">override</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ! (chyba p艡i p艡ekladu)</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>P艡i p艡ekladu dojde k chyb臎, proto啪e se sna啪铆me p艡edefinovat metodu <span class="geshifilter"><code class="text geshifilter-text">void f(int)</code></span> pomoc铆 <span class="geshifilter"><code class="text geshifilter-text">void f(double)</code></span>.</p> <p>Za pozornost stoj铆 fakt, 啪e se nejedn谩 o kl铆膷ov茅 slovo, n媒br啪 pouze identifik谩tor, kter媒 dostane speci谩ln铆 v媒znam a啪 v tomto kontextu. M暖啪ete si tak klidn臎 vytvo艡it prom臎nnou pojmenovanou <span class="geshifilter"><code class="text geshifilter-text">override</code></span>, co啪 by nebylo mo啪n茅, pokud by <span class="geshifilter"><code class="text geshifilter-text">override</code></span> bylo kl铆膷ov茅 slovo.</p> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3272.htm">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final">1</a>, <a href="http://en.cppreference.com/w/cpp/language/override">2</a>, <a href="https://blogs.oracle.com/pcarlini/entry/c_11_tidbits_explicit_overrides">3</a></p> <h3><strong>Zamezen铆 d臎d臎n铆 t艡铆d (final)</strong></h3> <p>M臎jme t艡铆du <span class="geshifilter"><code class="text geshifilter-text">NotBase</code></span>, u kter茅 chceme, aby ji nikdo nepou啪铆val jako b谩zovou t艡铆du, tj. chceme zak谩zat d臎d臎n铆 od t茅to t艡铆dy:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">class</span> NotBase <span style="color: #008000;">{</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">class</span> Derived<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> NotBase <span style="color: #008000;">{</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Toto nechceme povolit.</span></pre></div> <p>V C++98 toto lze vynutit <a href="http://www.parashift.com/c++-faq-lite/final-classes.html">t艡emi zp暖soby</a>: (1) naps谩n铆m koment谩艡e k b谩zov茅 t艡铆d臎, 啪e si nep艡ejeme, aby ji n臎kdo d臎dil. To n谩m ale nepom暖啪e proti ostr媒m hoch暖m, kte艡铆 koment谩艡e ignoruj铆. D谩le (2) t铆m, 啪e konstruktor ud臎l谩me priv谩tn铆 a poskytneme statickou vytv谩艡ec铆 funkci, a (3) velmi technick媒m zp暖sobem, kter媒 vyu啪铆v谩 virtu谩ln铆 d臎di膷nost.</p> <p>V C++11 je situace jednodu拧拧铆:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">class</span> NotBase <span style="color: #0000ff;">final</span> <span style="color: #008000;">{</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">class</span> Derived<span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> NotBase <span style="color: #008000;">{</span><span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ! (chyba p艡i p艡ekladu)</span></pre></div> <p>Za n谩zev t艡铆dy se d谩 identifik谩tor <span class="geshifilter"><code class="text geshifilter-text">final</code></span>, kter媒 zaru膷铆, 啪e od t艡铆dy nep暖jde d臎dit. Op臎t se nejedn谩 o kl铆膷ov茅 slovo, pouze o identifik谩tor.</p> <p>Odkazy: <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final">1</a>, <a href="http://en.cppreference.com/w/cpp/language/final">2</a></p> <h3><strong>Zamezen铆 redefinice virtu谩ln铆ch metod (final)</strong></h3> <p>Obdobn媒 probl茅m nast谩v谩 v C++98, pokud chceme zaru膷it, 啪e zvolenou virtu谩ln铆 metodu ji啪 nikdo nebude moct redefinovat (angl. override). M暖啪ete nam铆tat, 啪e pak sta膷铆 ud臎lat tuto metodu jako nevirtu谩ln铆 膷i ozna膷it celou t艡铆du jako fin谩ln铆 (viz v媒拧e), ale co kdy啪 se jedn谩 o jednu z podt艡铆d, u kter茅 chceme umo啪nit d臎d臎d铆 i redefinici jin媒ch metod, jen ne jedn茅 konkr茅tn铆? V C++98 toto nen铆 mo啪n茅.</p> <p>Zde nastupuje C++11, kde s vyu啪it铆m identifik谩toru <span class="geshifilter"><code class="text geshifilter-text">final</code></span> lze zamezit mo啪nosti jej铆ho redefinov谩n铆 v podt艡铆d谩ch:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">class</span> Base <span style="color: #008000;">{</span> <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> f<span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">class</span> Derived <span style="color: #008000;">{</span> <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> f<span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #0000ff;">final</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">class</span> MoreDerived <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Derived <span style="color: #008000;">{</span> <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> f<span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ! (chyba p艡i p艡ekladu)</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>Op臎t se jedn谩 pouze o identifik谩tor, nikoliv o kl铆膷ov茅 slovo.</p> <p>Odkazy: <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final">1</a>, <a href="http://en.cppreference.com/w/cpp/language/final">2</a></p> <h3><strong>Explicitn臎 zru拧en茅 metody</strong></h3> <p>膶astou situac铆 v C++ je, 啪e chceme zaru膷it, aby na拧e t艡铆da nebyla kop铆rovateln谩 (nap艡. pro implementaci tzv. <a href="http://c2.com/cgi/wiki?ReferenceObject">reference objects</a>). V C++98 se toho dalo dos谩hnout tak, 啪e se zprivatizoval kop铆rovac铆 konstruktor a oper谩tor p艡i艡azen铆 a vynechala se jejich definice:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">class</span> NonCopyable <span style="color: #008000;">{</span> <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span> NonCopyable<span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> NonCopyable<span style="color: #000040;">&</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// bez definice</span> NonCopyable<span style="color: #000040;">&</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> NonCopyable<span style="color: #000040;">&</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// bez definice</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>Jeliko啪 je kop铆rovac铆 konstruktor a oper谩tor p艡i艡azen铆 priv谩tn铆, nelze je pou啪铆t zvenku, a pokus o kopii instance t艡铆dy <span class="geshifilter"><code class="text geshifilter-text">NonCopyable</code></span> skon膷铆 s chybou p艡i p艡ekladu. Vynech谩n铆 definice t臎chto metod m谩 za n谩sledek, 啪e instance nep暖jde kop铆rovat ani uvnit艡 jin媒ch metod v t茅to t艡铆d臎.</p> <p>V C++11 je situace o n臎co jednodu拧拧铆:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">class</span> NonCopyable <span style="color: #008000;">{</span> <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span> NonCopyable<span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> NonCopyable<span style="color: #000040;">&</span><span style="color: #008000;">)</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">delete</span><span style="color: #008080;">;</span> NonCopyable<span style="color: #000040;">&</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> NonCopyable<span style="color: #000040;">&</span><span style="color: #008000;">)</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">delete</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>Ob臎 metody mohou z暖stat ve艡ejn茅, jen se za jejich deklarac铆 uvede kl铆膷ov茅 slovo <span class="geshifilter"><code class="text geshifilter-text">delete</code></span>, kter茅 zp暖sob铆, 啪e metody nep暖jdou pou啪铆t. Jejich pou啪it铆 toti啪 zp暖sob铆 chybu p艡i p艡ekladu.</p> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Explicitly_defaulted_and_deleted_special_member_functions">1</a>, <a href="http://stackoverflow.com/questions/823935/whats-the-point-in-defaulting-functions-in-c11">2</a></p> <h3><strong>Typ long long int</strong></h3> <p>V C++11 se objevuje typ <span class="geshifilter"><code class="text geshifilter-text">long long int</code></span>, kter媒 byl poprv茅 zaveden v C99. D暖vodem jeho p艡id谩n铆 bylo, aby byl i v C++ typ, kter媒 m谩 minim谩ln臎 64 bit暖. V C90 a C++98 byl toti啪 nejv臎t拧铆 typ <span class="geshifilter"><code class="text geshifilter-text">long int</code></span>, jeho啪 velikost byla zaru膷en臎 alespo艌 takov谩, jako velikost <span class="geshifilter"><code class="text geshifilter-text">int</code></span>u, a musel b媒t minim谩ln臎 32 bitov媒.</p> <p>U <span class="geshifilter"><code class="text geshifilter-text">long long int</code></span> je normou zaru膷eno, 啪e je minim谩ln臎 tak velk媒, jako <span class="geshifilter"><code class="text geshifilter-text">long int</code></span> a 啪e jeho velikost je minim谩ln臎 64 bit暖.</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> std<span style="color: #008080;">::</span><span style="color: #007788;">numeric_limits</span><span style="color: #000080;"><</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span><span style="color: #000080;">></span><span style="color: #008080;">::</span><span style="color: #007788;">min</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Alespo艌 -9223372036854775808 (2^63).</span> std<span style="color: #008080;">::</span><span style="color: #007788;">numeric_limits</span><span style="color: #000080;"><</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span><span style="color: #000080;">></span><span style="color: #008080;">::</span><span style="color: #007788;">max</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Alespo艌 9223372036854775807 (2^63 - 1).</span> std<span style="color: #008080;">::</span><span style="color: #007788;">numeric_limits</span><span style="color: #000080;"><</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span><span style="color: #000080;">></span><span style="color: #008080;">::</span><span style="color: #007788;">min</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 0</span> std<span style="color: #008080;">::</span><span style="color: #007788;">numeric_limits</span><span style="color: #000080;"><</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> <span style="color: #0000ff;">int</span><span style="color: #000080;">></span><span style="color: #008080;">::</span><span style="color: #007788;">max</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Alespo艌 18446744073709551615 (2^64 - 1).</span></pre></div> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1811.pdf">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Type_long_long_int">1</a>, <a href="http://en.cppreference.com/w/cpp/language/types">2</a></p> <h3><strong>Aserce p艡i p艡ekladu</strong></h3> <p>C++98 podporuje n谩sleduj铆c铆 dva druh媒 asserc铆:</p> <ul> <li><strong>B臎hem preprocesingu.</strong> Pomoc铆 direktiv <span class="geshifilter"><code class="text geshifilter-text">#if</code></span> a <span class="geshifilter"><code class="text geshifilter-text">#error</code></span>.</li> <li><strong>Za b臎hu</strong>. Pomoc铆 makra <span class="geshifilter"><code class="text geshifilter-text">assert()</code></span>.</li> </ul> <p>Ji啪 ale neobsahuje vestav臎nou podporu pro aserce za p艡ekladu. C++11 toto napravuje zaveden铆m kl铆膷ov茅ho slova <span class="geshifilter"><code class="text geshifilter-text">static_assert</code></span>. Form谩t pou啪it铆 je obdobn媒 klasick茅mu assertu:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">static_assert</span><span style="color: #008000;">(</span>konstantn铆 v媒raz, chybov谩 zpr谩va<span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>N谩sleduje uk谩zka pou啪it铆:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">template</span><span style="color: #000080;"><</span><span style="color: #0000ff;">class</span> Integral<span style="color: #000080;">></span> Integral mod<span style="color: #008000;">(</span>Integral x, Integral y<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">static_assert</span><span style="color: #008000;">(</span>std<span style="color: #008080;">::</span><span style="color: #007788;">is_integral</span><span style="color: #000080;"><</span>Integral<span style="color: #000080;">></span><span style="color: #008080;">::</span><span style="color: #007788;">value</span>, <span style="color: #FF0000;">"mod() parameter must be of an integral type"</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">return</span> x <span style="color: #000040;">%</span> y<span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>V t茅to 拧ablon臎 jsme pou啪ili aserci p艡i p艡ekladu, 啪e 拧ablonov媒 typ je n臎kter媒 z vestav臎n媒ch celo膷铆seln媒ch typ暖. Vyu啪铆v谩me tam tzv. typov媒 trait (angl. type trait) <a href="http://en.cppreference.com/w/cpp/types/is_integral"><span class="geshifilter"><code class="text geshifilter-text">std::is_integral<></code></span></a>.</p> <p>Pokud se pokus铆me zavolat na拧i 拧ablonovou funkci <span class="geshifilter"><code class="text geshifilter-text">mod()</code></span> s hodnotami typu <span class="geshifilter"><code class="text geshifilter-text">double</code></span>, dostaneme chybov茅 hl谩拧en铆:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;">mod<span style="color: #008000;">(</span><span style="color:#800080;">1.2</span>, <span style="color:#800080;">2.2</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ! (chyba p艡i p艡ekladu)</span> <span style="color: #666666;">// In instantiation of `Integral mod(Integral, Integral) [with Integral = double]':</span> <span style="color: #666666;">// error: static assertion failed: mod() parameter must be of an integral type</span></pre></div> <p>V C茅膷ku 膷i d艡铆v臎j拧铆ch verz铆ch se musely aserce p艡i p艡ekladu 艡e拧it oklikou. Jednu z nich budu prezentovat na p艡铆kladu. M臎jme kus k贸du, kde si chceme p艡i p艡ekladu ov臎艡it, 啪e <span class="geshifilter"><code class="text geshifilter-text">long int</code></span> m谩 alespo艌 64 bit暖. Lze to ud臎lat n谩sledovn臎 (jak v C, tak v C++):</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C99, C++98</span> <span style="color: #0000ff;">int</span> StaticAssertFailed_ExpectedLongToBeAtLeast64b<span style="color: #008000;">[</span><span style="color: #008000;">(</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">(</span><span style="color: #0000ff;">long</span><span style="color: #008000;">)</span> <span style="color: #000080;">>=</span> <span style="color: #0000dd;">8</span><span style="color: #008000;">)</span> <span style="color: #008080;">?</span> <span style="color: #0000dd;">1</span> <span style="color: #008080;">:</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">]</span><span style="color: #008080;">;</span></pre></div> <p>Pokud plat铆, 啪e <span class="geshifilter"><code class="text geshifilter-text">long int</code></span> m谩 alespo艌 64 bit暖, tak se vytvo艡铆 pole o jednom prvku. Pokud toto ale neplat铆, tak dojde k pokusu o vytvo艡en铆 pole o z谩porn茅m po膷tu prvk暖, co啪 啪谩dn谩 z norem nedovoluje. V媒sledkem pak bude chybov茅 hl谩拧en铆 podobn茅 tomuto:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// error: size of array `StaticAssertFailed_ExpectedLongToBeAtLeast64b' is negative</span></pre></div> <p>Z n臎j je vid臎t, pro膷 jsme zvolili takov茅 "拧铆len茅" jm茅no prom臎nn茅. Krom臎 tohoto p艡铆stupu existuje i cel谩 艡ada jin媒ch p艡铆stup暖, ale 啪谩dn媒 z nich nen铆 tak 膷ist媒, jako <span class="geshifilter"><code class="text geshifilter-text">static_assert</code></span> v C++11.</p> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1720.html">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Static_assertions">1</a></p> <h3><strong>R-hodnotov茅 reference</strong></h3> <p>C++11 zav谩d铆 krom臎 klasick媒ch referenc铆 tzv. r-hodnotov茅 reference (angl. rvalue references), kter茅 se daj铆 nav谩zat na do膷asn茅 objekty. Syntaktick谩 odli拧enost je v tom, 啪e se pou啪ije jeden ampersand nav铆c. 膶ili, <span class="geshifilter"><code class="text geshifilter-text">&& T</code></span> zna膷铆 r-hodnotovou referenci na typ <span class="geshifilter"><code class="text geshifilter-text">T</code></span>. Vyu啪it铆 r-hodnotov媒ch referenc铆 je p艡edev拧铆m pro urychlen铆 b臎hu programu pomoc铆 mo啪nosti p艡esunu objekt暖 na m铆sto jejich kop铆rov谩n铆, 膷emu啪 se 艡铆k谩 anglicky <a href="http://www.artima.com/cppsource/rvalue.html">move semantics</a> a uk谩啪eme si to d谩le na p艡铆kladu. Dal拧铆mu z vyu啪it铆, <a href="http://www.artima.com/cppsource/rvalue.html">perfect forwarding</a>, se zde v臎novat nebudeme.</p> <p>Dejme tomu, 啪e chcete v C++98 nadefinovat 拧ablonu <span class="geshifilter"><code class="text geshifilter-text">swap<></code></span> pro prohozen铆 dvou objekt暖. Standardn铆 mo啪nost, jak ji naimplementovat, je n谩sleduj铆c铆:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">template</span> <span style="color: #000080;"><</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">></span> swap<span style="color: #008000;">(</span>T<span style="color: #000040;">&</span> a, T<span style="color: #000040;">&</span> b<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> T tmp<span style="color: #008000;">(</span>a<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// zkop铆ruje a</span> a <span style="color: #000080;">=</span> b<span style="color: #008080;">;</span> <span style="color: #666666;">// zkop铆ruje b</span> b <span style="color: #000080;">=</span> tmp<span style="color: #008080;">;</span> <span style="color: #666666;">// zkop铆ruje tmp</span> <span style="color: #008000;">}</span></pre></div> <p>Probl茅m t茅to implementace je, 啪e se vytv谩艡铆 zbyte膷n茅 kopie. V拧e, co chceme, je p艡esun dvou objekt暖 - nap艡. na 艡谩dku <span class="geshifilter"><code class="text geshifilter-text">T tmp(a);</code></span> nepot艡ebujeme vytvo艡en铆 kopie, proto啪e v z谩p臎t铆 na dal拧铆m 艡谩dku do prom臎nn茅 <span class="geshifilter"><code class="text geshifilter-text">a</code></span> zkop铆rujeme <span class="geshifilter"><code class="text geshifilter-text">b</code></span>.</p> <p>V C++11 to lze vy艡e拧it za pomoc铆 r-hodnotov媒ch referenc铆 a standardn铆 funkce <span class="geshifilter"><code class="text geshifilter-text">std::move()</code></span>, kter谩 n谩m umo啪n铆 napsat n谩sleduj铆c铆 variantu:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">template</span> <span style="color: #000080;"><</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">></span> swap<span style="color: #008000;">(</span>T<span style="color: #000040;">&</span> a, T<span style="color: #000040;">&</span> b<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> T tmp<span style="color: #008000;">(</span>std<span style="color: #008080;">::</span><span style="color: #007788;">move</span><span style="color: #008000;">(</span>a<span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// bez kopie</span> a <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">move</span><span style="color: #008000;">(</span>b<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// bez kopie</span> b <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">move</span><span style="color: #008000;">(</span>tmp<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// bez kopie</span> <span style="color: #008000;">}</span></pre></div> <p>Zde se nevytvo艡铆 ani jedna kopie, pouze se p艡esouvaj铆 "vnit艡nosti" objekt暖. Samoz艡ejm臎, p艡edan茅 objekty mus铆 takov媒 p艡esun vnit艡nost铆 podporovat.</p> <p>Za r-hodnotov媒mi referencemi je toho mnohem v铆c, tak啪e ur膷it臎 mrkn臎te na odkazy n铆啪e.</p> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2118.html">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Rvalue_references_and_move_constructors">1</a>, <a href="http://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers">2</a>, <a href="http://www.artima.com/cppsource/rvalue.html">3</a>, <a href="http://stackoverflow.com/questions/6829241/perfect-forwarding-whats-it-all-about">4</a></p> <h3><strong>Zobecn臎n茅 konstantn铆 v媒razy</strong></h3> <p>M臎jme n谩sleduj铆c铆 艡谩dek k贸du v C++98:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> d <span style="color: #000080;">=</span> <span style="color: #0000dd;">sqrt</span><span style="color: #008000;">(</span><span style="color:#800080;">5.6</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>K vy膷铆slen铆 odmocniny z <span class="geshifilter"><code class="text geshifilter-text">5.6</code></span> dojde a啪 za b臎hu programu. D暖vodem je, 啪e p艡eklada膷 nen铆 povinen d臎lat vyhodnocen铆 vol谩n铆 funkc铆 za p艡ekladu, a nav铆c v n臎kter媒ch situac铆ch ani nem暖啪e, proto啪e nem谩 dostatek informac铆 od program谩tora 膷i mu to norma jazyka zakazuje. M暖啪ete nam铆tat, 啪e sta膷铆, aby vy膷铆slen铆 provedl program谩tor a dal do k贸du p艡铆mo v媒sledek, ale to m暖啪e vy煤stit ve sn铆啪enou 膷itelnost. Ide谩ln铆 by bylo, kdyby se poda艡ilo, aby podobn谩 vol谩n铆 dok谩zal vy膷铆slit p艡铆mo p艡eklada膷.</p> <p>Obdobn谩 situace nast谩v谩, kdy啪 si chcete nadefinovat pole, jeho啪 rozm臎r je d谩n vol谩n铆m funkce, by钮 vracej铆c铆 v啪dy stejn媒 v媒sledek:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">int</span> f<span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> <span style="color: #0000ff;">int</span> arr<span style="color: #008000;">[</span>f<span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008000;">]</span><span style="color: #008080;">;</span> <span style="color: #666666;">// ! (chyba p艡i p艡ekladu)</span></pre></div> <p>Vol谩n铆 funkce toti啪 nen铆 pova啪ov谩no za konstantn铆 v媒raz, i kdyby funkce v啪dy vracela stejn媒 v媒sledek.</p> <p>C++11 oba p艡铆pady 艡e拧铆 p艡id谩n铆m kl铆膷ov茅ho slova <span class="geshifilter"><code class="text geshifilter-text">constexpr</code></span>, kter茅 zna膷铆, 啪e dan谩 funkce vrac铆 konstantn铆 v媒raz. Pokud tedy vhodn臎 nadefinujeme funkci <span class="geshifilter"><code class="text geshifilter-text">sqrt</code></span>, nap艡. takto:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">constexpr</span> <span style="color: #0000ff;">double</span> <span style="color: #0000dd;">abs</span><span style="color: #008000;">(</span><span style="color: #0000ff;">double</span> x<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">return</span> <span style="color: #008000;">(</span>x <span style="color: #000080;">></span> <span style="color:#800080;">0.0</span> <span style="color: #008080;">?</span> x <span style="color: #008080;">:</span> <span style="color: #000040;">-</span>x<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> <span style="color: #0000ff;">constexpr</span> <span style="color: #0000ff;">double</span> sqrtImpl<span style="color: #008000;">(</span><span style="color: #0000ff;">double</span> y, <span style="color: #0000ff;">double</span> x1, <span style="color: #0000ff;">double</span> x2, <span style="color: #0000ff;">double</span> eps<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">return</span> <span style="color: #008000;">(</span><span style="color: #0000dd;">abs</span><span style="color: #008000;">(</span>x1 <span style="color: #000040;">-</span> x2<span style="color: #008000;">)</span> <span style="color: #000080;"><</span> eps <span style="color: #008080;">?</span> x2 <span style="color: #008080;">:</span> sqrtImpl<span style="color: #008000;">(</span>y, x2, <span style="color: #008000;">(</span>x2 <span style="color: #000040;">+</span> y <span style="color: #000040;">/</span> x2<span style="color: #008000;">)</span> <span style="color: #000040;">*</span> <span style="color:#800080;">0.5</span>, eps<span style="color: #008000;">)</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> <span style="color: #0000ff;">constexpr</span> <span style="color: #0000ff;">double</span> <span style="color: #0000dd;">sqrt</span><span style="color: #008000;">(</span><span style="color: #0000ff;">double</span> y<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">return</span> sqrtImpl<span style="color: #008000;">(</span>y, <span style="color: #0000dd;">0</span>, y <span style="color: #000040;">*</span> <span style="color:#800080;">0.5</span> <span style="color: #000040;">+</span> <span style="color:#800080;">1.0</span>, <span style="color:#800080;">1e-10</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>pak v n谩sleduj铆c铆m p艡铆pad臎</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> d <span style="color: #000080;">=</span> <span style="color: #0000dd;">sqrt</span><span style="color: #008000;">(</span><span style="color:#800080;">5.6</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// vyhodnoceno p艡i p艡ekladu</span></pre></div> <p>dojde k vyhodnocen铆 vol谩n铆 za p艡ekladu, tak啪e se t铆m program nemus铆 zdr啪ovat za b臎hu. Samoz艡ejm臎, 啪e u takov茅hoto jednoduch茅ho vol谩n铆 to moc smysl ned谩v谩, ale pokud by se n臎co po膷铆talo v cyklu 膷i rekurzi, tak to m暖啪e zna膷n臎 urychlit b臎h programu.</p> <p>U p艡铆kladu s polem je situace obdobn谩 - sta膷铆 nadefinovat <span class="geshifilter"><code class="text geshifilter-text">f()</code></span> jako <span class="geshifilter"><code class="text geshifilter-text">constexpr</code></span> funkci:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">constexpr</span> <span style="color: #0000ff;">int</span> f<span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> <span style="color: #0000ff;">int</span> arr<span style="color: #008000;">[</span>f<span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008000;">]</span><span style="color: #008080;">;</span> <span style="color: #666666;">// OK</span></pre></div> <p>P艡eklada膷 pak ji啪 nek艡i膷铆 a k贸d je korektn铆 dle normy C++11.</p> <p>Na z谩v臎r bych zm铆nil, 啪e pomoc铆 tzv. <a href="http://en.wikipedia.org/wiki/Template_metaprogramming">template meta-programming</a>, <span class="geshifilter"><code class="text geshifilter-text">constexpr</code></span> a vyu啪it铆 toho, 啪e <a href="/?q=node/22">拧ablony v C++ jsou v媒po膷etn臎 煤pln茅</a>, lze d臎lat opravdu ps铆 kusy :). Lze doj铆t k zaj铆mav媒m situac铆m, nap艡. 啪e p艡eklad k贸du, kter媒 m谩 cca 13 艡谩dek, bude trvat i 20 hodin.</p> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2235.pdf">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#constexpr_-_Generalized_constant_expressions">1</a>, <a href="http://www.codeproject.com/Articles/417719/Constants-and-Constant-Expressions-in-Cplusplus11">2</a>, <a href="http://paulkeir.wordpress.com/2012/09/02/a-compile-time-fft-in-c11/">3</a></p> <p>Nyn铆 se zam臎艡铆me na novinky ze sv臎ta 拧ablon.</p> <h3><strong>Dvojit茅 煤hlov茅 z谩vorky u 拧ablon</strong></h3> <p>Pokud jste cht臎li v C++98 vytvo艡it 拧ablonu, jej铆啪 拧ablonov媒 typ je 拧ablona, tak jste narazil na men拧铆 lexik谩ln铆 nep艡铆jemnost, a to tu, 啪e mezi 煤hlov媒mi z谩vorkami ukon膷uj铆c铆mi z谩pis musela b媒t ponech谩na mezera:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span><span style="color: #0000ff;">int</span><span style="color: #000080;">>></span> <span style="color: #666666;">// !</span> std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span><span style="color: #0000ff;">int</span><span style="color: #000080;">></span> <span style="color: #000080;">></span> <span style="color: #666666;">// OK</span></pre></div> <p>P艡eklada膷 toti啪 dv臎 ukon膷uj铆c铆 煤hlov茅 z谩vorky za sebou bere jako oper谩tor bitov茅ho posunu doprava, co啪 je v臎t拧inou ne啪谩douc铆 token a m谩 za n谩sledek syntaktickou chybu. Jinak, ne啪 pou啪it铆m mezery nav铆c, to vy艡e拧it ne拧lo.</p> <p>V C++11 odpad谩 nutnost pou啪铆t onu otravnou mezeru nav铆c:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;"><</span><span style="color: #0000ff;">int</span><span style="color: #000080;">>></span> <span style="color: #666666;">// OK</span></pre></div> <p>Hur谩 :). Pokud by i p艡esto n臎kdo pot艡eboval p暖vodn铆 chov谩n铆, tak se <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Right_angle_bracket">d谩 vynutit pou啪it铆m klasick媒ch z谩vorek</a>.</p> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1757.html">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Right_angle_bracket">1</a>, <a href="http://allanmcrae.com/2012/05/c11-part-4-template-tidbits/">2</a></p> <h3><strong>Aliasy na 拧ablony</strong></h3> <p>Dejme tomu, 啪e byste si cht臎li vytvo艡it slovn铆k, tj. mapov谩n铆 艡et臎zce na n臎co jin茅ho, co bude parametrizovan茅. Mohlo by v谩s napadnou vyu啪铆t <span class="geshifilter"><code class="text geshifilter-text">std::map<></code></span> a <span class="geshifilter"><code class="text geshifilter-text">typedef</code></span>, ov拧em n臎co takov茅ho neprojde p艡es p艡eklada膷:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">template</span> <span style="color: #000080;"><</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">></span> <span style="color: #0000ff;">typedef</span> std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;"><</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span>, T<span style="color: #000080;">></span> Dictionary<span style="color: #008080;">;</span> <span style="color: #666666;">// ! (chyba p艡i p艡ekladu)</span> <span style="color: #666666;">// Takto byste to cht臎li pou啪铆t:</span> <span style="color: #666666;">// Dictionary<int> d;</span></pre></div> <p>V C++11 se na tom, 啪e tato konstrukce je neplatn谩, nic nem臎n铆. C++11 v拧ak zav谩d铆 novou syntaxi, pomoc铆 kter茅 m暖啪eme dos谩hnout k媒啪en茅ho v媒sledku:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">template</span> <span style="color: #000080;"><</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">></span> <span style="color: #0000ff;">using</span> Dictionary <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;"><</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span>, T<span style="color: #000080;">></span><span style="color: #008080;">;</span> <span style="color: #666666;">// OK</span> Dictionary<span style="color: #000080;"><</span><span style="color: #0000ff;">int</span><span style="color: #000080;">></span> d<span style="color: #008080;">;</span></pre></div> <p>Tuto syntaxi lze pou啪铆vat na m铆sto <span class="geshifilter"><code class="text geshifilter-text">typedef</code></span> nejen v p艡铆pad臎 拧ablon:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> std<span style="color: #008080;">::</span><span style="color: #007788;">ios_base</span><span style="color: #008080;">::</span><span style="color: #007788;">fmtflags</span> flags<span style="color: #008080;">;</span> <span style="color: #666666;">// star谩 syntaxe</span> <span style="color: #0000ff;">using</span> flags <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">ios_base</span><span style="color: #008080;">::</span><span style="color: #007788;">fmtflags</span><span style="color: #008080;">;</span> <span style="color: #666666;">// nov谩 syntaxe</span> <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">void</span> <span style="color: #008000;">(</span><span style="color: #000040;">*</span>func<span style="color: #008000;">)</span><span style="color: #008000;">(</span><span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">int</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// star谩 syntaxe</span> <span style="color: #0000ff;">using</span> func <span style="color: #000080;">=</span> <span style="color: #0000ff;">void</span> <span style="color: #008000;">(</span><span style="color: #000040;">*</span><span style="color: #008000;">)</span><span style="color: #008000;">(</span><span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">int</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// nov谩 syntaxe</span></pre></div> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Template_aliases">1</a>, <a href="http://allanmcrae.com/2012/05/c11-part-4-template-tidbits/">2</a>, <a href="https://blogs.oracle.com/pcarlini/entry/template_aliases">3</a>, <a href="http://en.cppreference.com/w/cpp/language/type_alias">4</a>, <a href="http://thenewcpp.wordpress.com/2011/11/08/template-aliases/">5</a>, <a href="http://loungecpp.wikidot.com/tips-and-tricks:reducing-pain-with-template-aliases">6</a>, <a href="http://stackoverflow.com/questions/4152976/flexibility-of-template-alias-in-c0x">7</a></p> <h3><strong>艩ablony s prom臎nn媒m po膷tem typov媒ch parametr暖</strong></h3> <p>Dejme tomu, 啪e chcete v C++98 napsat funkci, kter谩 V谩m vytiskne to, co ji p艡ed谩te, a钮 u啪 j铆 p艡ed谩te dva argumenty 膷i deset argument暖. Pou啪it铆 funkc铆 s prom臎nn媒m po膷tem parametr暖 nen铆 to prav茅 o艡铆拧kov茅, proto啪e ty vy啪aduj铆 alespo艌 jeden fixn铆 parametr, podle kter茅ho se zjist铆, kolik argument暖 se m谩 zpracovat a jak茅 maj铆 typy. Jedna z mo啪nost铆 je n谩sleduj铆c铆:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++98</span> <span style="color: #0000ff;">template</span> <span style="color: #000080;"><</span><span style="color: #0000ff;">typename</span> T1<span style="color: #000080;">></span> <span style="color: #0000ff;">void</span> print<span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> T1<span style="color: #000040;">&</span> val1<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;"><<</span> val1 <span style="color: #000080;"><<</span> <span style="color: #FF0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> <span style="color: #0000ff;">template</span> <span style="color: #000080;"><</span><span style="color: #0000ff;">typename</span> T1, <span style="color: #0000ff;">typename</span> T2<span style="color: #000080;">></span> <span style="color: #0000ff;">void</span> print<span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> T1<span style="color: #000040;">&</span> val1, <span style="color: #0000ff;">const</span> T2<span style="color: #000040;">&</span> val2<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;"><<</span> val1<span style="color: #008080;">;</span> print<span style="color: #008000;">(</span>val2<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> <span style="color: #0000ff;">template</span> <span style="color: #000080;"><</span><span style="color: #0000ff;">typename</span> T1, <span style="color: #0000ff;">typename</span> T2, <span style="color: #0000ff;">typename</span> T3<span style="color: #000080;">></span> <span style="color: #0000ff;">void</span> print<span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> T1<span style="color: #000040;">&</span> val1, <span style="color: #0000ff;">const</span> T2<span style="color: #000040;">&</span> val2, <span style="color: #0000ff;">const</span> T3<span style="color: #000040;">&</span> val3<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;"><<</span> val1<span style="color: #008080;">;</span> print<span style="color: #008000;">(</span>val2, val3<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> <span style="color: #666666;">// ...</span> print<span style="color: #008000;">(</span><span style="color: #FF0000;">"I am "</span>, <span style="color: #0000dd;">26</span>, <span style="color: #FF0000;">" years old."</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// I am 26 years old.</span></pre></div> <p>Samoz艡ejm臎, kdy啪 chcete podporovat a啪 deset parametr暖, mus铆te si ud臎lat p艡et铆啪en茅 verze pro 膷ty艡i, p臎t, ..., a啪 deset parametr暖. To je velmi n谩ro膷n茅 na psan铆 i na 煤dr啪bu, nehled臎 na to, 啪e kdy啪 si n臎kdo bude cht铆t vytisknout jeden谩ct objekt暖, bude m铆t sm暖lu.</p> <p>C++11 艡e拧铆 tento probl茅m zaveden铆m 拧ablon s prom臎nn媒m po膷tem parametr暖. Ona funkce <span class="geshifilter"><code class="text geshifilter-text">print()</code></span> by v C++11 mohla vypadat takto:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">template</span> <span style="color: #000080;"><</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">></span> <span style="color: #0000ff;">void</span> print<span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">&</span> value<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;"><<</span> value <span style="color: #000080;"><<</span> <span style="color: #FF0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> <span style="color: #0000ff;">template</span> <span style="color: #000080;"><</span><span style="color: #0000ff;">typename</span> U, <span style="color: #0000ff;">typename</span>... <span style="color: #007788;">T</span><span style="color: #000080;">></span> <span style="color: #0000ff;">void</span> print<span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> U<span style="color: #000040;">&</span> head, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&</span>... <span style="color: #007788;">tail</span><span style="color: #008000;">)</span> <span style="color: #008000;">{</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;"><<</span> head<span style="color: #008080;">;</span> print<span style="color: #008000;">(</span>tail...<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> print<span style="color: #008000;">(</span><span style="color: #FF0000;">"I am "</span>, <span style="color: #0000dd;">26</span>, <span style="color: #FF0000;">" years old."</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #666666;">// I am 26 years old.</span></pre></div> <p>V拧imn臎te si vyu啪it铆 rekurze, kter茅 p艡ipom铆n谩 zpracov谩n铆 seznam暖 v <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29">Haskellu</a> 膷i <a href="http://en.wikipedia.org/wiki/Prolog">Prologu</a>.</p> <p>艩ablony s prom臎nn媒m po膷tem parametr暖 se hod铆 v mnoha p艡铆padech. Jedn铆m z p艡铆pad暖 jsou ntice (viz d谩le).</p> <p>Odkazy: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2242.pdf">0</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Variadic_templates">1</a>, <a href="http://www.cplusplus.com/articles/EhvU7k9E/">2</a>, <a href="http://stackoverflow.com/questions/5625600/what-is-the-meaning-of-token">3</a>, <a href="http://oopscenities.net/2011/07/19/c0x-variadic-templates-functions/">4</a></p> <p>Nyn铆 p艡ejdeme na n臎kter茅 novinky ze standardn铆 knihovny.</p> <h3><strong>Ntice</strong></h3> <p>Prvn铆 novinkou ve standardn铆 knihovn臎 C++11 jsou ntice. Jedn谩 se o jednodu拧拧铆 obdobu struktur, k jej铆m啪 prvk暖m se p艡istupuje pomoc铆 index暖. Pokud m谩te ntici <span class="geshifilter"><code class="text geshifilter-text">t</code></span>, tak k <span class="geshifilter"><code class="text geshifilter-text">N</code></span>t茅mu prvku se dostanete pomoc铆 <span class="geshifilter"><code class="text geshifilter-text">get<N>(t)</code></span>.</p> <p>P艡铆klad:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">tuple</span><span style="color: #000080;"><</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span>, <span style="color: #0000ff;">int</span><span style="color: #000080;">></span> getAddressAndPort<span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&</span> url<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #666666;">// zpracov谩n铆 url a z铆sk谩n铆 adresy a portu</span> <span style="color: #666666;">// ...</span> <span style="color: #0000ff;">return</span> std<span style="color: #008080;">::</span><span style="color: #007788;">make_tuple</span><span style="color: #008000;">(</span>address, port<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> host<span style="color: #008080;">;</span> <span style="color: #0000ff;">int</span> port<span style="color: #008080;">;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">tie</span><span style="color: #008000;">(</span>host, port<span style="color: #008000;">)</span> <span style="color: #000080;">=</span> getAddressAndPort<span style="color: #008000;">(</span><span style="color: #FF0000;">"127.0.0.1:631"</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <p>V tomto p艡铆kladu m谩me funkci, kter谩 n谩m z p艡edan茅ho URL z铆sk谩 adresu a port (p艡铆klad je to dost um臎l媒, ale je na n臎m vid臎t princip). Jeliko啪 tato funkce m谩 vracet v铆ce hodnot, m暖啪eme vyu啪铆t ntic. Ntice se d谩 snadno vytvo艡it pomoc铆 funkce <span class="geshifilter"><code class="text geshifilter-text">std::make_tuple()</code></span>, u kter茅 nen铆 t艡eba p艡ed谩vat typov茅 parametry (jedn谩 se ve skute膷nosti o 拧ablonu). Ke snadn茅mu ulo啪en铆 v媒sledku do dvou prom臎nn媒ch pak lze vyu啪铆t standardn铆 funkci <span class="geshifilter"><code class="text geshifilter-text">std::tie()</code></span>, kter谩 n谩m nav谩啪e prvky ntice na na拧e prom臎nn茅.</p> <p>Vyu啪it铆 ntic:</p> <ul> <li><strong>Funkce vracej铆c铆 v铆ce hodnot</strong>. Toto vyu啪it铆 jsme vid臎li v posledn铆m p艡铆kladu.</li> <li><strong>V媒m臎na hodnot</strong>. Pokud m谩te v铆ce hodnot, kter茅 chcete ur膷it媒m zp暖sobem vym臎nit, lze to ud臎lat takovouto fintou :):<br> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> std<span style="color: #008080;">::</span><span style="color: #007788;">tie</span><span style="color: #008000;">(</span>a, b, c<span style="color: #008000;">)</span> <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">make_tuple</span><span style="color: #008000;">(</span>b, c, a<span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> </li> </ul> <p>Odkazy: <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Tuple_types">1</a>, <a href="http://stackoverflow.com/questions/855110/why-is-the-use-of-tuples-in-c-not-more-common">2</a>, <a href="http://arkaitzj.wordpress.com/2009/11/20/c-tuples-a-quick-overview/">3</a>, <a href="http://www.codeguru.com/cpp/article.php/c18275/C-Tutorial-Use-stdtuple-To-Simulate-Compact-Heterogeneous-Containers.htm">4</a>, <a href="http://stackoverflow.com/questions/3313479/stdtuple-get-member-function">5</a></p> <h3><strong>Hashovac铆 tabulky</strong></h3> <p>Dal拧铆 novinkou jsou implementace standardn铆ch kontejner暖 pro mno啪iny a mapy za pomoc铆 hashovac铆ch tabulek. U operac铆 kontejner暖 <span class="geshifilter"><code class="text geshifilter-text">std::map<></code></span> a <span class="geshifilter"><code class="text geshifilter-text">std::set<></code></span> toti啪 standard vy啪aduje jist茅 vlastnosti z hlediska 膷asov茅 slo啪itosti, kter茅 je velmi t臎啪k茅 splnit bez pou啪it铆 jin茅, ne啪 stromov茅 reprezentace. Tyto kontejnery tak b媒vaj铆 implementov谩ny pomoc铆 n臎kter茅 varianty <a href="http://en.wikipedia.org/wiki/Avl_tree">AVL stromu</a>.</p> <p>Verze standardn铆ch kontejner暖, kter茅 jsou implementov谩ny pomoc铆 hashovac铆ch tabulek, lze nal茅zt v hlavi膷kov茅m souboru <span class="geshifilter"><code class="text geshifilter-text"><unordered_{set,multiset,map,multimap}></code></span>. Tento n谩zev byl zvolen z toho d暖vodu, aby nedo拧lo ke kolizi s ji啪 existuj铆c铆mi implementacemi hashovac铆ch tabulek, kter茅 p艡eklada膷e b臎啪n臎 nab铆z铆. Kl铆膷e u t臎chto kontejner暖 mus铆 b媒t <a href="http://en.wikipedia.org/wiki/Hash_function">hashovateln茅</a> a porovnateln茅 mezi sebou. To, 啪e mus铆 b媒t hashovateln茅, je z艡ejm茅. To, 啪e mus铆 b媒t i porovnateln茅, vypl媒v谩 z toho, 啪e na jeden kl铆膷 se m暖啪e namapovat v铆ce polo啪ek, a my pot艡ebujeme zjistit, zda n臎kter谩 z polo啪ek, kter茅 maj铆 stejn媒 hash, je ta, kterou chceme vkl谩dat/vyhled谩vat. Rozhran铆 je obdobn茅, jako maj铆 jejich nehashovac铆 prot臎j拧ky.</p> <p>Poj膹me se pod铆vat na srovn谩n铆 膷asov茅 slo啪itosti operac铆 nad <span class="geshifilter"><code class="text geshifilter-text">std::set</code></span> (stromov谩 implementace) a <span class="geshifilter"><code class="text geshifilter-text">std::unordered_set</code></span> (implementace hashovac铆 tabulkou); <span class="geshifilter"><code class="text geshifilter-text">n</code></span> zna膷铆 po膷et prvk暖 v kontejneru. V p艡铆pad臎 <span class="geshifilter"><code class="text geshifilter-text">std::set</code></span> trvaj铆 operace vlo啪en铆, p艡id谩n铆, vymaz谩n铆 a vyhled谩n铆 <span class="geshifilter"><code class="text geshifilter-text">O(log(n))</code></span> (logaritmick谩 slo啪itost), kde啪to v p艡铆pad臎 <span class="geshifilter"><code class="text geshifilter-text">std::unordered_set</code></span> pouze <span class="geshifilter"><code class="text geshifilter-text">O(1)</code></span> (konstantn铆 膷asov谩 slo啪itost). Kde je h谩膷ek? Ten je v tom, 啪e u <span class="geshifilter"><code class="text geshifilter-text">std::unordered_set</code></span> se jedn谩 o slo啪itost v pr暖m臎rn茅m p艡铆pad臎. V nejhor拧铆m p艡铆pad臎, pokud se v拧echny prvky mapuj铆 na to stejn茅 m铆sto, je slo啪itost <span class="geshifilter"><code class="text geshifilter-text">O(n)</code></span>, co啪 je lin谩rn铆 (hor拧铆 ne啪 logaritmick谩).</p> <p>Odkazy: <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Hash_tables">1</a>, <a href="http://www.cplusplus.com/reference/unordered_set/unordered_set/">2</a></p> <h3><strong>Chytr茅 ukazatele</strong></h3> <p>Klasick茅 C茅膷kov茅 ukazatele (angl. raw pointers) maj铆 mimo sv媒ch nezanedbateln媒ch v媒hod (nap艡. rychlost) 艡adu neduh:</p> <ul> <li>Nen铆 jist茅, kdo vlastn铆 alokovanou pam臎钮 a kdo m谩 ukazatel uvolnit.</li> <li>Nen铆 jasn茅, kdy by se m臎la alokovan谩 pam臎钮 uvolnit.</li> <li>Alokovan谩 pam臎钮 mus铆 b媒t dealokovan谩 manu谩ln臎.</li> </ul> <p>C++11 se tyto neduhy sna啪铆 艡e拧it pomoc铆 tzv. chytr媒ch ukazatel暖 (angl. <a href="http://en.wikipedia.org/wiki/Smart_pointer">smart pointers</a>). Vezm臎me si n谩sleduj铆c铆 p艡铆klad, kter媒 vyu啪铆v谩 jeden z chytr媒ch ukazatel暖 dostupn媒ch v C++11:</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> CarFactory <span style="color: #008000;">{</span> <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span> <span style="color: #0000ff;">static</span> std<span style="color: #008080;">::</span><span style="color: #007788;">unique_ptr</span><span style="color: #000080;"><</span>Car<span style="color: #000080;">></span> create<span style="color: #008000;">(</span><span style="color: #ff0000; font-style: italic;">/* parametery */</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span></pre></div> <p>V媒拧e uveden谩 tov谩rna podle p艡edan媒ch parametr暖 vytvo艡铆 auto a vr谩t铆 na n臎j ukazatel. T铆mto k贸dem 艡铆k谩me dv臎 v臎ci: (1) ten, kdo funkci zavol谩, se st谩v谩 jedin媒m vlastn铆kem ukazatele a mus铆 se o n臎j postarat (tj. ukazatel nen铆 ulo啪en a pou啪铆v谩n nikde jinde) a (2) po konci bloku, kde je ukazatel vytvo艡en, dojde k automatick茅mu uvoln臎n铆 pam臎ti, na kter媒 ukazuje.</p> <p>Celkem jsou v C++11 k dispozici 膷ty艡i chytr茅 ukazatele, kde ka啪d媒 z nich m谩 jin茅 pou啪it铆 a vlastnosti:</p> <ul> <li><span class="geshifilter"><code class="text geshifilter-text">std::unique_ptr</code></span> - jedine膷n媒 ukazatel na objekt. Nen铆 kop铆rovateln媒, pouze p艡esouvateln媒.</li> <li><span class="geshifilter"><code class="text geshifilter-text">std::shared_ptr</code></span> - sd铆len媒 ukazatel.</li> <li><span class="geshifilter"><code class="text geshifilter-text">std::weak_ptr</code></span> - "slab拧铆" obdoba sd铆len茅ho ukazatele, jeho啪 jedn铆m z 煤kol暖 je 艡e拧it probl茅my s cyklick媒mi z谩vislostmi (chytr茅 ukazatele v C++11 toti啪 vyu啪铆vaj铆 tzv. <a href="http://en.wikipedia.org/wiki/Reference_counting">reference counting</a>, kter茅mu d臎laj铆 probl茅my cykly, kdy na sebe cyklicky navz谩jem ukazuje v铆ce objekt暖).</li> <li><span class="geshifilter"><code class="text geshifilter-text">std::auto_ptr</code></span> - p艡e啪itek z C++98. Od C++11 je ozna膷en jako zastaral媒 (angl. deprecated). M铆sto n臎j byste m臎li pou啪铆vat <span class="geshifilter"><code class="text geshifilter-text">std::unique_ptr</code></span> (<a href="http://stackoverflow.com/questions/111478/why-is-it-wrong-to-use-stdauto-ptr-with-standard-containers"><span class="geshifilter"><code class="text geshifilter-text">std::auto_ptr</code></span> se toti啪 ned谩 uchov谩vat v kontejnerech</a>).</li> </ul> <p>Odkazy: <a href="http://en.wikipedia.org/wiki/Smart_pointer">1</a>, <a href="http://en.cppreference.com/w/cpp/memory/unique_ptr""">2</a></p> <h3><strong>Regul谩rn铆 v媒razy</strong></h3> <p>Posledn铆 novinka, kterou si zm铆n铆me, je podpora pro regul谩rn铆 v媒razy. Regul谩rn铆 v媒razy v C++11 jsou zalo啪eny na regul谩rn铆ch v媒razech v <a href="http://www.boost.org/doc/libs/1_52_0/libs/regex/doc/html/index.html"><span class="geshifilter"><code class="text geshifilter-text">Boost.regex</code></span></a> a podporu pro n臎 lze nal茅zt v hlavi膷kov茅m souboru <span class="geshifilter"><code class="text geshifilter-text"><regex></code></span>. Je podporov谩no 拧est r暖zn媒ch druh暖 syntaxe, z nich啪 implicitn铆 je <a href="http://www.cplusplus.com/reference/regex/ECMAScript/">ECMAScript syntaxe</a> (podob谩 se syntaxi regul谩rn铆ch v媒raz暖 v Perlu).</p> <p>V C++11 jsou k dispozici n谩sleduj铆c铆 algoritmy:</p> <ul> <li><span class="geshifilter"><code class="text geshifilter-text">std::regex_match()</code></span> - zji拧t臎n铆, zda p艡edan媒 艡et臎zec p艡esn臎 odpov铆d谩 zadan茅mu regul谩rn铆mu v媒razu</li> <li><span class="geshifilter"><code class="text geshifilter-text">std::regex_search()</code></span> - vyhled谩v谩n铆 zadan茅ho regul谩rn铆ho v媒razu v p艡edan茅m 艡et臎zci</li> <li><span class="geshifilter"><code class="text geshifilter-text">std::regex_replace()</code></span> - nahrazov谩n铆 dle zadan茅ho regul谩rn铆ho v媒razu</li> </ul> <p>Mrkneme se na p艡铆klad. Jeliko啪 je v拧ak podpora regul谩rn铆ch v媒raz暖 ve standardn铆 knihovn臎 na Linuxu (<a href="http://gcc.gnu.org/libstdc++/">libstdc++</a>) prozat铆m velmi mizern谩, tak neru膷铆m, 啪e bude fungovat korektn臎 :).</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #666666;">// C++11</span> <span style="color: #0000ff;">void</span> showIPParts<span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&</span> ip<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> std<span style="color: #008080;">::</span><span style="color: #007788;">regex</span> pattern<span style="color: #008000;">(</span><span style="color: #FF0000;">R"((\d{1,3}):(\d{1,3}):(\d{1,3}):(\d{1,3}))"</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">match_results</span><span style="color: #000080;"><</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span><span style="color: #000080;">></span> result<span style="color: #008080;">;</span> <span style="color: #0000ff;">bool</span> valid <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">regex_match</span><span style="color: #008000;">(</span>ip, result, pattern<span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">(</span>valid<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;"><<</span> result<span style="color: #008000;">[</span><span style="color: #0000dd;">0</span><span style="color: #008000;">]</span> <span style="color: #000080;"><<</span> <span style="color: #ff0000; font-style: italic;">/* ... */</span> <span style="color: #000080;"><<</span> <span style="color: #FF0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> <span style="color: #008000;">}</span></pre></div> <p>V拧imn臎te si pou啪it铆 nov茅ho 艡et臎zcov茅ho liter谩lu, prefixovan媒m p铆smenem <span class="geshifilter"><code class="text geshifilter-text">R</code></span>. V takov茅m tzv. raw liter谩lu nen铆 t艡eba prefixovat lom铆tka a uvozovky. To se obzvl谩拧t臎 u regul谩rn铆ch v媒raz暖 velmi hod铆. Vytvo艡铆me si zjednodu拧en媒 regul谩rn铆 v媒raz pro IP adresu, vytvo艡铆me si pomocnou prom臎nnou pro ulo啪en铆 v媒sledku, provedeme matching pomoc铆 <span class="geshifilter"><code class="text geshifilter-text">std::regex_match()</code></span> a zkontrolujeme, zda 艡et臎zec odpov铆d谩 regul谩rn铆mu v媒razu. Pokud ano, pak v <span class="geshifilter"><code class="text geshifilter-text">result</code></span> m谩me k dispozici v拧echny skupiny, kter茅 jsme uvedli v regul谩rn铆m v媒razu. Ka啪d谩 skupina koresponduje k jednomu oktetu.</p> <p>Odkazy: <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Regular_expressions">1</a>, <a href="http://en.wikipedia.org/wiki/C%2B%2B11#New_string_literals (raw strings)">2</a>, <a href="http://en.cppreference.com/w/cpp/regex">3</a>, <a href="http://solarianprogrammer.com/2011/10/12/cpp-11-regex-tutorial/">4</a>, <a href="http://www.cpprocks.com/regex-cheatsheet/">5</a>, <a href="http://www.georgeflanagin.com/c++11/regex.php">6</a>, <a href="http://www.johndcook.com/cpp_regex.html">7</a>, <a href="http://stackoverflow.com/questions/9746409/why-does-c11-support-6-different-regular-expression-grammars">8</a>, <a href="http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339/A-TR1-Tutorial-Regular-Expressions.htm">9</a></p> <h2>Co jsme p艡esko膷ili a nepro拧li</h2> <p>Jeliko啪 je toho v nov茅 norm臎 opravdu hodn臎, tak jsem spoustu novinek p艡esko膷il. N铆啪e uv谩d铆m seznam n臎kter媒ch z nich, abyste si o nich mohli vyhledat informace na Internetu.</p> <p><strong>Aktualizace 6.10.2015:</strong> V臎t拧inu z p艡esko膷en媒ch novinek n铆啪e jsem popsal v <a href="/2015-10-06-mene-zname-novinky-v-cpp11-a-cpp14">navazuj铆c铆m p艡铆sp臎vku</a>.</p> <ul> <li>extern铆 拧ablony</li> <li>tzv. inline prostory jmen</li> <li>modifikovan谩 definice POD</li> <li><span class="geshifilter"><code class="text geshifilter-text">sizeof</code></span> funguje na 膷lenech t艡铆d bez nutnosti m铆t k dispozici instanci</li> <li>dal拧铆 kl铆膷ov谩 slova: <span class="geshifilter"><code class="text geshifilter-text">alignof</code></span>, <span class="geshifilter"><code class="text geshifilter-text">alignas</code></span>, <span class="geshifilter"><code class="text geshifilter-text">noexcept</code></span></li> <li>umo啪艌uje implementace s garbage collectorem</li> <li>podpora pro v铆cevl谩knov茅 programov谩n铆</li> <li>unicode 艡et臎zcov茅 liter谩ly (utf-8, utf-16, utf-32)</li> <li>u啪ivatelsky definovan茅 liter谩ly</li> <li>neomezen茅 unie</li> <li>explicitn铆 konverzn铆 oper谩tory</li> <li>roz拧铆艡iteln谩 podpora pro gener谩tory n谩hodn媒ch 膷铆sel</li> <li>tzv. wrapper reference</li> <li>polymorfn铆 wrappery pro funk膷n铆 objekty</li> <li>tzv. type traits pro 拧ablonov茅 metaprogramov谩n铆</li> <li>jednotn谩 metoda pro v媒po膷et n谩vratov茅ho typu u funktor暖</li> <li>vylep拧en铆 konstrukce objekt暖</li> <li>... a mnoho dal拧铆ch</li> </ul> <h2>Co z C++98 byste v C++11 nem臎li pou啪铆vat</h2> <p>N谩sleduj铆c铆 z谩le啪itost byla ze standardu odstran臎na:</p> <ul> <li><strong>Exportovan茅 拧ablony</strong>. Jedn谩 se kl铆膷ov茅 slovo <span class="geshifilter"><code class="text geshifilter-text">export</code></span>, kter茅 umo啪艌uje odd臎lit deklaraci 拧ablony od jej铆 implementace. Kdy啪 toti啪 definujete 拧ablonu, tak jej铆 definice mus铆 b媒t p艡铆stupn谩 v拧em, kte艡铆 ji cht臎j铆 pou啪铆vat, proto se jej铆 definice d谩v谩 do hlavi膷kov媒ch soubor. Jak poznamenal dr. Peringer od n谩s z fakulty, tak toto kl铆膷ov茅 slovo se hod铆 pro komer膷n铆 firmy, kter媒m vad铆 "nucen媒 open-source" jejich k贸du, kter媒 poskytuje 拧ablony :) (ve skute膷nosti to tento probl茅m tak 煤pln臎 ne艡e拧铆, viz detaily v uveden媒ch 膷l谩nc铆ch d谩le). Ve拧lo ve zn谩most jako kl铆膷ov茅 slovo, kter茅 m谩 nejmen拧铆 podporu v p艡eklada膷铆ch (drtiv谩 v臎t拧ina p艡eklada膷暖 jej neimplementuje, v膷etn臎 gcc, kv暖li n谩ro膷nosti implementace a/nebo kv暖li tomu, 啪e toho ve skute膷nosti moc nep艡in谩拧铆), tak啪e se s n铆m t茅m臎艡 nesetk谩te. Pokud by v谩s zaj铆maly detaily o tom, co ve skute膷nosti export znamen谩 a jak茅 zde existuj铆 probl茅my, tak v艡ele doporu膷uji <a href="http://www.ddj.com/cpp/184401563">tento</a> a <a href="http://www.ddj.com/showArticle.jhtml?articleID=184401584">tento</a> 膷l谩nek od Herba Suttera.</li> </ul> <p>A n谩sleduj铆c铆 z谩le啪itosti byly zase ozna膷eny jako zastaral茅 (angl. deprecated):</p> <ul> <li><strong>Dynamick谩 specifikace v媒jimek</strong>. <a href="http://www.gotw.ca/publications/mill22.htm">Nechovalo se to tak, jak by 膷lov臎k o膷ek谩val.</a></li> <li><strong><span class="geshifilter"><code class="text geshifilter-text">std::auto_ptr</code></span></strong>. M铆sto n臎j pou啪铆vejte <span class="geshifilter"><code class="text geshifilter-text">std::unique_ptr</code></span>, kter媒 lze pou啪铆vat v kontejnerech (<a href="http://stackoverflow.com/questions/111478/why-is-it-wrong-to-use-stdauto-ptr-with-standard-containers"><span class="geshifilter"><code class="text geshifilter-text">std::auto_ptr</code></span> ne</a>).</li> <li><strong>A dal拧铆</strong>. Nap艡铆klad b谩zov茅 t艡铆dy pro funk膷n铆 objekty, adapt茅ry, bindery. V C++ jsou za n臎 lep拧铆 n谩hrady, nap艡. <a href="http://en.wikipedia.org/wiki/C%2B%2B11#Polymorphic_wrappers_for_function_objects">polymorfn铆 wrappery</a> a <a href="http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B">lambda funkce</a>.</li> </ul> <p>Ur膷it臎 si stoj铆 za to p艡e膷铆st <a href="http://stackoverflow.com/a/9338032">tento p艡铆sp臎vek</a>, kde jeho autor zmi艌uje 艡adu idiom暖, kter媒 lze v C++11 艡e拧it elegantn臎ji.</p> <h2>Podpora v p艡eklada膷铆ch, standardn铆ch knihovn谩ch a n谩stroj铆ch</h2> <p>Na z谩v臎r se mrkneme, jak je to s podporou nov茅 normy v p艡eklada膷铆ch, standardn铆ch knihovn谩ch a jin媒ch n谩stroj铆ch, kter茅 pracuj铆 se zdrojov媒mi k贸dy.</p> <h3><strong>Podpora v p艡eklada膷铆ch</strong></h3> <p><a href="http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport">Zde</a> je k dispozici p艡ehledn谩 tabulka, kter谩 ukazuje, co z nov茅 normy C++ je ji啪 podporov谩no v p艡eklada膷铆ch. Z tabulky je vid臎t, 啪e nejv铆ce z C++11 je k dispozici v <a href="http://gcc.gnu.org/projects/cxx0x.html">GCC</a> a <a href="http://clang.llvm.org/cxx_status.html">Clangu</a>.</p> <h3><strong>Podpora ve standardn铆ch knihovn谩ch</strong></h3> <p>Pokud je sou膷谩st铆 p艡eklada膷e i implementace standardn铆 knihovny, pak nen铆 co 艡e拧it, ale t艡eba na Linuxu p艡eklada膷e vyu啪铆vaj铆 extern铆 standardn铆 knihovnu. Mezi nejroz拧铆艡en臎j拧铆 pat艡铆 <a href="http://gcc.gnu.org/libstdc++/">libstdc++</a>, kterou vyu啪铆v谩 <span class="geshifilter"><code class="text geshifilter-text">GCC</code></span> a <span class="geshifilter"><code class="text geshifilter-text">Clang</code></span>. Podpora C++11 v t茅to knihovn臎 je zobrazena <a href="http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html">v t茅to tabulce</a>. Krom臎 regul谩rn铆ch v媒raz暖 a podpory pro v铆cevl谩knov茅 zpracov谩n铆 je toho v臎t拧ina naimplementov谩na.</p> <p><span class="geshifilter"><code class="text geshifilter-text">Clang</code></span> za膷al pracovat na sv茅 vlastn铆 implementaci standardn铆 knihovny. Stav t茅to implementace je k nahl茅dnut铆 <a href="http://libcxx.llvm.org/">zde</a>. Jak je vid臎t, dost toho chyb铆, tak啪e moment谩ln臎 doporu膷uji s <span class="geshifilter"><code class="text geshifilter-text">Clang</code></span>em pou啪铆vat <span class="geshifilter"><code class="text geshifilter-text">libstdc++</code></span>.</p> <p>Co se t媒膷e MS windows a standardn铆 knihovny pro Visual Studio, tak mrkn臎te <a href="http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx">zde</a>.</p> <h3><strong>Podpora v ostatn铆ch n谩stroj铆ch</strong></h3> <p>Kdy啪 u啪 za膷nete vyu啪铆vat C++11, tak je dobr茅, aby tuto novou normu podporovaly i n谩stroje. Jedn铆m z hojn臎 vyu啪铆van媒ch n谩stroj暖 pro tvorbu projektov茅 dokumentace a popis <a href="http://en.wikipedia.org/wiki/Application_programming_interface">API</a> je program <a href="http://www.stack.nl/~dimitri/doxygen/">doxygen</a>. Ten um铆 z komentovan媒ch zdrojov媒ch k贸d暖 vygenerovat popis modul暖, t艡铆d, funkc铆 apod.</p> <p>Doxygen podporuje C++11 od verze <a href="http://www.stack.nl/~dimitri/doxygen/changelog.html">1.8.2</a>, vydan茅 v srpnu tohoto roku. <a href="http://www.stack.nl/~dimitri/doxygen/changelog.html">Zde</a> je k nahl茅dnut铆 p艡ehled zm臎n.</p> <h3><strong>Z谩v臎r</strong></h3> <p>Douf谩m, 啪e se v谩m m暖j p艡铆sp臎vek ukazuj铆c铆 n臎kter茅 z novinek v nov茅 norm臎 ISO C++11 l铆bil. Podpora v p艡eklada膷铆ch a standardn铆ch knihovn谩ch se ka啪d媒m m臎s铆cem zlep拧uje a ji啪 te膹 m暖啪ete za膷铆st sm臎le vyu啪铆vat mnoha vlastnost铆, kter茅 nov谩 norma p艡in谩拧铆.</p> <p>Pokud v谩s n臎co z m茅ho p艡铆sp臎vku mimo艡谩dn臎 zaujalo, ur膷it臎 to napi拧te do koment谩艡e. Mo啪n谩 se k tomu pak vr谩t铆m v n臎kter茅m z nadch谩zej铆c铆ch p艡铆sp臎vk暖 :).</p> </div> <div class="field field--name-taxonomy-vocabulary-1 field--type-entity-reference field--label-inline clearfix"> <div class="field__label">Tagy</div> <div class="field__items"> <div class="field__item"><a href="/taxonomy/term/7" hreflang="cs">programov谩n铆</a></div> <div class="field__item"><a href="/taxonomy/term/13" hreflang="cs">C++</a></div> </div> </div> <section data-drupal-selector="comments" class="comments"> <div class="add-comment"> <div class="add-comment__form"> <form class="comment-comment-node-blog-form comment-form" data-user-info-from-browser data-drupal-selector="comment-form" action="/comment/reply/node/116/comment_node_blog" method="post" id="comment-form" accept-charset="UTF-8"> <div class="js-form-item form-item js-form-type-textfield form-item-name js-form-item-name"> <label for="edit-name" class="form-item__label js-form-required form-required">Va拧e jm茅no</label> <input data-drupal-default-value="Anonym" data-drupal-selector="edit-name" type="text" id="edit-name" name="name" value="" size="30" maxlength="60" class="form-text required form-element form-element--type-text form-element--api-textfield" required="required" aria-required="true" /> </div> <div class="js-form-item form-item js-form-type-email form-item-mail js-form-item-mail"> <label for="edit-mail" class="form-item__label js-form-required form-required">E-mail</label> <input data-drupal-selector="edit-mail" aria-describedby="edit-mail--description" type="email" id="edit-mail" name="mail" value="" size="30" maxlength="64" class="form-email required form-element form-element--type-email form-element--api-email" required="required" aria-required="true" /> <div id="edit-mail--description" class="form-item__description description"> Obsah tohoto pole je soukrom媒 a nebude ve艡ejn臎 zobrazen. </div> </div> <div class="js-form-item form-item js-form-type-url form-item-homepage js-form-item-homepage"> <label for="edit-homepage" class="form-item__label">Domovsk谩 str谩nka</label> <input data-drupal-selector="edit-homepage" type="url" id="edit-homepage" name="homepage" value="" size="30" maxlength="255" class="form-url form-element form-element--type-url form-element--api-url" /> </div> <div class="field--type-text-long field--name-comment-body field--widget-text-textarea js-form-wrapper form-wrapper" data-drupal-selector="edit-comment-body-wrapper" id="edit-comment-body-wrapper"> <div class="js-text-full text-full js-form-item form-item" data-drupal-selector="edit-comment-body-0"> <div class="js-form-item form-item js-form-type-textarea form-item-comment-body-0-value js-form-item-comment-body-0-value"> <label for="edit-comment-body-0-value" class="form-item__label js-form-required form-required">Koment谩艡</label> <div> <textarea class="js-text-full text-full form-textarea required form-element form-element--type-textarea form-element--api-textarea" data-drupal-selector="edit-comment-body-0-value" id="edit-comment-body-0-value" name="comment_body[0][value]" rows="5" cols="60" placeholder="" required="required" aria-required="true"></textarea> </div> </div> <div class="js-filter-wrapper filter-wrapper js-form-wrapper form-wrapper" data-drupal-selector="edit-comment-body-0-format" id="edit-comment-body-0-format"><div data-drupal-selector="edit-comment-body-0-format-help" class="filter-help js-form-wrapper form-wrapper" id="edit-comment-body-0-format-help"><a href="/filter/tips" target="_blank" data-drupal-selector="edit-comment-body-0-format-help-about" id="edit-comment-body-0-format-help-about">O form谩tech textu</a></div> <div class="js-filter-guidelines filter-guidelines js-form-wrapper form-wrapper" data-drupal-selector="edit-comment-body-0-format-guidelines" id="edit-comment-body-0-format-guidelines"> <div data-drupal-format-id="1" class="filter-guidelines__item filter-guidelines__item--_"> <h4 class="label">Filtrovan茅 HTML (vyu啪铆v谩no)</h4> <ul class="filter-tips filter-tips--short"> <li class="filter-tips__item filter-tips__item--short">Povolen茅 HTML zna膷ky: <a href hreflang> <em> <strong> <cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <table></li> <li class="filter-tips__item filter-tips__item--short">Zv媒razn臎n铆 syntaxe k贸du lze povolit p艡es n谩sleduj铆c铆 zna膷ky: <code><code></code>, <code><blockcode></code>, <code><bash></code>, <code><c></code>, <code><cpp></code>, <code><haskell></code>, <code><html></code>, <code><java></code>, <code><javascript></code>, <code><latex></code>, <code><perl></code>, <code><php></code>, <code><python></code>, <code><ruby></code>, <code><rust></code>, <code><sql></code>, <code><text></code>, <code><vim></code>, <code><xml></code>, <code><yaml></code>.</li> <li class="filter-tips__item filter-tips__item--short">艠谩dky a odstavce se zalom铆 automaticky.</li> <li class="filter-tips__item filter-tips__item--short">Webov茅 a e-mailov茅 adresy jsou automaticky p艡evedeny na odkazy.</li> </ul> </div> </div> </div> </div> </div> <input data-drupal-selector="form-1clt0kcv7nrm5dnri4um6pfgtnm-cjn9ywjyagcmabc" type="hidden" name="form_build_id" value="form-1clT0KcV7NrM5DNRI4Um6PfgTNM-cjn9yWJyagcMABc" /> <input data-drupal-selector="edit-comment-comment-node-blog-form" type="hidden" name="form_id" value="comment_comment_node_blog_form" /> <div class="field--type-string field--name-subject field--widget-string-textfield js-form-wrapper form-wrapper" data-drupal-selector="edit-subject-wrapper" id="edit-subject-wrapper"> <div class="js-form-item form-item js-form-type-textfield form-item-subject-0-value js-form-item-subject-0-value"> <label for="edit-subject-0-value" class="form-item__label">P艡edm臎t</label> <input class="js-text-full text-full form-text form-element form-element--type-text form-element--api-textfield" data-drupal-selector="edit-subject-0-value" type="text" id="edit-subject-0-value" name="subject[0][value]" value="" size="60" maxlength="64" placeholder="" /> </div> </div> <fieldset data-drupal-selector="edit-captcha" class="captcha captcha-type-challenge--math"> <legend class="captcha__title js-form-required form-required"> CAPTCHA </legend> <div class="captcha__element"> <input data-drupal-selector="edit-captcha-sid" type="hidden" name="captcha_sid" value="1440114" /> <input data-drupal-selector="edit-captcha-token" type="hidden" name="captcha_token" value="ocJrSJVsOwhT3nJF9q98VbrvBjcPM1RhSLmFraboP1k" /> <div class="js-form-item form-item js-form-type-textfield form-item-captcha-response js-form-item-captcha-response"> <label for="edit-captcha-response" class="form-item__label js-form-required form-required">Matematick媒 p艡铆klad</label> <span class="field-prefix">9 + 3 =</span> <input autocomplete="off" data-drupal-selector="edit-captcha-response" aria-describedby="edit-captcha-response--description" type="text" id="edit-captcha-response" name="captcha_response" value="" size="4" maxlength="2" class="form-text required form-element form-element--type-text form-element--api-textfield" required="required" aria-required="true" /> <div id="edit-captcha-response--description" class="form-item__description description"> Vy艡e拧te tento jednoduch媒 matematick媒 p艡铆klad a vlo啪te v媒sledek. Nap艡. pro 1+3 vlo啪te 4. </div> </div> </div> <div class="captcha__description description">N臎jak se mi tady rozmohl spam, tak啪e popros铆m o ov臎艡en铆.</div> </fieldset> <div data-drupal-selector="edit-actions" class="form-actions js-form-wrapper form-wrapper" id="edit-actions"><input data-drupal-selector="edit-preview" type="submit" id="edit-preview" name="op" value="N谩hled" class="button js-form-submit form-submit" /> </div> </form> </div> </div> <article data-comment-user-id="0" id="comment-2702" class="comment js-comment comment--level-1 by-anonymous" role="article" data-drupal-selector="comment"> <span class="hidden" data-comment-timestamp="1473093022"></span> <div class="comment__picture-wrapper"> <div class="comment__picture"> </div> </div> <div class="comment__text-wrapper"> <footer class="comment__meta"> <p class="comment__author"><span>Filip Kesner (neov臎艡eno)</span></p> <p class="comment__time">8 years 2 months zp臎t</p> </footer> <div class="comment__content"> <h3><a href="/comment/2702#comment-2702" class="permalink" rel="bookmark" hreflang="und">Compile error</a></h3> <div class="text-content field field--name-comment-body field--type-text-long field--label-hidden field__item comment__text-content"><p>Mozna mi neco unika, ale nasledujici kod nejde prelozit.<br> Varianta s predavanim hodnotou, se ovsem uspesne prelozi.<br> Mozna mi neco unika ...</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Person<span style="color: #008000;">{</span> <span style="color: #0000ff;">int</span> id<span style="color: #008080;">;</span> string name<span style="color: #008080;">;</span> <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span> <span style="color: #0000ff;">int</span> getId<span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">return</span> id<span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">sort</span><span style="color: #008000;">(</span> people.<span style="color: #007788;">begin</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span>, people.<span style="color: #007788;">end</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span>, <span style="color: #008000;">[</span><span style="color: #008000;">]</span><span style="color: #008000;">(</span><span style="color: #0000ff;">const</span> Person<span style="color: #000040;">&</span> p1, <span style="color: #0000ff;">const</span> Person<span style="color: #000040;">&</span> p2<span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #666666;">// anonymni funkce</span> <span style="color: #0000ff;">return</span> p1.<span style="color: #007788;">getId</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #000080;"><</span> p2.<span style="color: #007788;">getId</span><span style="color: #008000;">(</span><span style="color: #008000;">)</span><span style="color: #008080;">;</span> <span style="color: #008000;">}</span> <span style="color: #008000;">)</span><span style="color: #008080;">;</span></pre></div> <div class="geshifilter"> <pre class="text geshifilter-text" style="font-family:monospace;">g++ --std=c++11 new_things.cpp -o bin_new_things -Wall -Wno-unused-variable new_things.cpp: In lambda function: new_things.cpp:159:23: error: passing 'const Person' as 'this' argument of 'int Person::getId()' discards qualifiers [-fpermissive] return p1.getId() < p2.getId(); ^ new_things.cpp:159:36: error: passing 'const Person' as 'this' argument of 'int Person::getId()' discards qualifiers [-fpermissive] return p1.getId() < p2.getId(); ^ make: *** [all] Error 1</pre></div> </div> <ul class="links inline comment__links"> <li class="comment__links-item"><a href="/comment/reply/node/116/comment_node_blog/2702" class="comment__links-link">Odpov臎d臎t</a></li> </ul> </div> </div> </article> <div class="indented"> <article data-comment-user-id="1" id="comment-2703" class="comment js-comment by-node-author" role="article" data-drupal-selector="comment"> <span class="hidden" data-comment-timestamp="1473093010"></span> <div class="comment__picture-wrapper"> <div class="comment__picture"> </div> </div> <div class="comment__text-wrapper"> <footer class="comment__meta"> <p class="comment__author"><span>Petr Zemek</span></p> <p class="comment__time">8 years 2 months zp臎t</p> <p class="visually-hidden">In reply to <a href="/comment/2702#comment-2702" class="permalink" rel="bookmark" hreflang="und">Compile error</a> by <span>Filip Kesner (neov臎艡eno)</span></p> </footer> <div class="comment__content"> <h3><a href="/comment/2703#comment-2703" class="permalink" rel="bookmark" hreflang="und">Re: Compile error</a></h3> <div class="text-content field field--name-comment-body field--type-text-long field--label-hidden field__item comment__text-content"><p>Ahoj. Ona metoda <span class="geshifilter"><code class="text geshifilter-text">getId()</code></span> mus铆 b媒t ozna膷ena jako <span class="geshifilter"><code class="text geshifilter-text">const</code></span>, jinak ji nelze na objektu typu <span class="geshifilter"><code class="text geshifilter-text">const Person&</code></span> zavolat. 膶ili zm臎艌</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> getId<span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">return</span> id<span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>na</p> <div class="geshifilter"> <pre class="cpp geshifilter-cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> getId<span style="color: #008000;">(</span><span style="color: #008000;">)</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">{</span> <span style="color: #0000ff;">return</span> id<span style="color: #008080;">;</span> <span style="color: #008000;">}</span><span style="color: #008080;">;</span></pre></div> <p>a bude to fungovat.</p> </div> <ul class="links inline comment__links"> <li class="comment__links-item"><a href="/comment/reply/node/116/comment_node_blog/2703" class="comment__links-link">Odpov臎d臎t</a></li> </ul> </div> </div> </article> </div> </section> <ul class="links inline"> <li><a href="/2012-12-04-co-je-noveho-v-cpp11#comment-form" title="Share your thoughts and opinions." hreflang="cs">P艡idat koment谩艡</a></li> </ul> </div> </article> </div> </div> </div> </main> </div> </div> <div class="social-bar"> <div class="social-bar__inner fixable"> <div class="rotate"> <div id="block-olivero-syndicate" role="complementary" class="block block-node block-node-syndicate-block"> <div class="block__content"> <a href="/rss.xml" class="feed-icon"> <span class="feed-icon__label"> RSS zdroj </span> <span class="feed-icon__icon" aria-hidden="true"> <svg xmlns="http://www.w3.org/2000/svg" width="14.2" height="14.2" viewBox="0 0 14.2 14.2"> <path d="M4,12.2c0-2.5-3.9-2.4-3.9,0C0.1,14.7,4,14.6,4,12.2z M9.1,13.4C8.7,9,5.2,5.5,0.8,5.1c-1,0-1,2.7-0.1,2.7c3.1,0.3,5.5,2.7,5.8,5.8c0,0.7,2.1,0.7,2.5,0.3C9.1,13.7,9.1,13.6,9.1,13.4z M14.2,13.5c-0.1-3.5-1.6-6.9-4.1-9.3C7.6,1.7,4.3,0.2,0.8,0c-1,0-1,2.6-0.1,2.6c5.8,0.3,10.5,5,10.8,10.8C11.5,14.5,14.3,14.4,14.2,13.5z"/> </svg> </span> </a> </div> </div> </div> </div> </div> </div> </div> <footer class="site-footer"> <div class="site-footer__inner container"> <div class="region region--footer-bottom grid-full layout--pass--content-medium"> <div id="block-footer" class="block block-block-content block-block-content032bfd07-2293-4f6c-b766-46496fa5e118"> <div class="block__content"> <div class="text-content clearfix field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Copyright 漏 2008-2024 <a href="https://petrzemek.net/" title="Petr Zemek">Petr Zemek</a><br> Obsah blogu je licencov谩n pod licenc铆 <a href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0</a></p> <!-- Matomo --><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://piwik.petrzemek.net/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><p></p><noscript><img referrerpolicy="no-referrer-when-downgrade" src="https://piwik.petrzemek.net/matomo.php?idsite=1&rec=1" style="border:0" alt><noscript></noscript> <!-- End Matomo Code --></noscript></div> </div> </div> </div> </div> </footer> <div class="overlay" data-drupal-selector="overlay"></div> </div> </div> </div> <script type="application/json" data-drupal-selector="drupal-settings-json">{"path":{"baseUrl":"\/","pathPrefix":"","currentPath":"node\/116","currentPathIsAdmin":false,"isFront":false,"currentLanguage":"cs"},"pluralDelimiter":"\u0003","suppressDeprecationErrors":true,"ajaxTrustedUrl":{"\/search\/node":true,"\/comment\/reply\/node\/116\/comment_node_blog":true},"user":{"uid":0,"permissionsHash":"bbd2c54abf56605b0af36bc383d4ec31dc184a497ae6a55163982b96018d5061"}}</script> <script src="/core/assets/vendor/jquery/jquery.min.js?v=3.7.1"></script> <script src="/core/assets/vendor/once/once.min.js?v=1.0.1"></script> <script src="/sites/default/files/languages/cs_D93HdRZGpg7nhR6DhDGYlp0QHUP8cwKn4L9i_mznr9A.js?sncyar"></script> <script src="/core/misc/drupalSettingsLoader.js?v=10.3.2"></script> <script src="/core/misc/drupal.js?v=10.3.2"></script> <script src="/core/misc/drupal.init.js?v=10.3.2"></script> <script src="/core/assets/vendor/tabbable/index.umd.min.js?v=6.2.0"></script> <script src="/core/themes/olivero/js/navigation-utils.js?v=10.3.2"></script> <script src="/core/themes/olivero/js/checkbox.js?v=10.3.2"></script> <script src="/core/modules/comment/js/comment-by-viewer.js?v=10.3.10"></script> <script src="/core/themes/olivero/js/comments.js?v=10.3.2"></script> <script src="/core/themes/olivero/js/navigation.js?v=10.3.2"></script> <script src="/core/themes/olivero/js/second-level-navigation.js?v=10.3.2"></script> <script src="/core/themes/olivero/js/nav-resize.js?v=10.3.2"></script> <script src="/core/themes/olivero/js/search.js?v=10.3.2"></script> <script src="/core/misc/debounce.js?v=10.3.2"></script> <script src="/core/misc/form.js?v=10.3.2"></script> <script src="/core/modules/filter/filter.js?v=10.3.10"></script> </body> </html>