{"id":1107,"date":"2019-04-15T18:29:53","date_gmt":"2019-04-15T18:29:53","guid":{"rendered":"https:\/\/yazilimperver.net\/?p=1107"},"modified":"2019-04-18T20:22:52","modified_gmt":"2019-04-18T20:22:52","slug":"haftalik-c-14-stdoptional","status":"publish","type":"post","link":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/","title":{"rendered":"[:tr]Haftal\u0131k C++ 14 \u2013 std::optional[:en]Weekly C++ 14 \u2013 std::optional[:]"},"content":{"rendered":"<p>[:tr]<span style=\"font-weight: 400;\">Merhabalar arkada\u015flar, yeni bir haftal\u0131k C++ yaz\u0131m\u0131z ile birlikteyiz. Bu yaz\u0131m\u0131n konusu, C++ 17 ile birlikte dile dahil edilen <em>std::optional<\/em> yetene\u011fi. Bu kabiliyete neden ihtiyac\u0131m\u0131z var, nerelerde kullanabiliriz gibi sorulara \u00e7e\u015fitli kod \u00f6rnekleri \u00fczerinden giderek bakaca\u011f\u0131z. Bu yap\u0131 ile ilintili olarak <em>std::variant<\/em> ve <em>std::any<\/em> yap\u0131lar\u0131na da farkl\u0131 yaz\u0131lar\u0131mda de\u011finece\u011fim. O zaman hemen ba\u015flayal\u0131m ne dersiniz.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/images.unsplash.com\/photo-1493612276216-ee3925520721?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1000&amp;q=80\" alt=\"person holding light bulb\" width=\"430\" height=\"537\" \/><\/p>\n<p id=\"usaKdxl\">\u00d6ncelikli olarak neden b\u00f6yle bir yap\u0131ya ihtiyac\u0131m\u0131z var ona bakal\u0131m. Baz\u0131 durumlarda, de\u011fi\u015fkenlerde anlaml\u0131 bir veri olmad\u0131\u011f\u0131n\u0131 ifade etmek istedi\u011finiz emin olmu\u015ftur (komut sat\u0131r\u0131ndan girilen opsiyonel arg\u00fcmanlar ya da girilse de girilmese olabilecek alanlar, g\u00f6bek ismi gibi). Mevcut durumda, bunu yapman\u0131n elbette bir tak\u0131m yollar\u0131 var. Bunu i\u015faret\u00e7ilerde yapmak nispeten kolay, <i>nullptr <\/i>ile yapabilirsiniz ama bu ilklendirilmedi\u011fi anlam\u0131na m\u0131 gelir ya da anlaml\u0131 veri olmad\u0131\u011f\u0131na m\u0131? Di\u011fer tipler i\u00e7in durum biraz daha farkl\u0131. Tamsay\u0131 de\u011fi\u015fkenler i\u00e7in -1 ve benzeri de\u011ferler kullanabilirsiniz, bu durumda da art\u0131k -1 kullanamazs\u0131n\u0131z. Baz\u0131 durumlarda -1 gibi bir say\u0131 bulman\u0131z da zor olabilir ya da her seferinde bu ama\u00e7 i\u00e7in kullanabilece\u011finiz bir rakam bulmak zor olabilir. \u00d6r. \u00d6zel yap\u0131lar ya da ondal\u0131k say\u0131larda ne kullanaca\u011f\u0131z? Ayr\u0131ca bu tarz kullan\u0131mlarda, fazladan veri var m\u0131 ya da anlaml\u0131 m\u0131, kontrolleri yapman\u0131z gerekebilir (\u00f6r. \u00f6nce ilgili de\u011fi\u015fken ge\u00e7erli mi de\u011fil mi kontrol\u00fc, sonras\u0131nda e\u011fer ge\u00e7erli ise de\u011fi\u015fken de\u011ferini almak gibi).<\/p>\n<p><span style=\"font-weight: 400;\">\u0130\u015fte <em>std::optional<\/em>, tam da bu noktada imdad\u0131m\u0131za yeti\u015fiyor. Bu yap\u0131 ile birlikte, ilgili de\u011fi\u015fkenin olup olmad\u0131\u011f\u0131n\u0131\/herhangi bir de\u011fer i\u00e7erip i\u00e7ermedi\u011fini daha <\/span><b>anlaml\u0131 <\/b><span style=\"font-weight: 400;\">ve <\/span><b>standart <\/b><span style=\"font-weight: 400;\">bir \u015fekilde ger\u00e7ekle\u015ftirebilece\u011fiz. Bu kabiliyet \u00e7e\u015fitli kaynaklarda \u201cnullable types\u201d olarak da ge\u00e7mektedir. Giri\u015f k\u0131sm\u0131nda anlatt\u0131klar\u0131m\u0131z\u0131 \u00f6zetleyecek olursa, hi\u00e7bir de\u011fer almayacak tipleri ifade etmek i\u00e7in, bir i\u015flem sonucunda anlaml\u0131 bir d\u00f6n\u00fc\u015f olmayaca\u011f\u0131 durumlarda ya da fonksiyonlara bir de\u011fer ge\u00e7irme ya da ge\u00e7irmeme durumlar\u0131 s\u00f6z konusu oldu\u011funda kullan\u0131labilir.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><em>std::optional<\/em>, di\u011fer bir tak\u0131m kabiliyet gibi, <em>boost::optional<\/em>\u2019\u0131 baz almakta, o sebeple e\u011fer onu kulland\u0131ysan\u0131z, ge\u00e7i\u015finiz \u00e7ok zor olmayacakt\u0131r (bu arada boost kullan\u0131m\u0131na ili\u015fkin \u00f6rnek bir kaynak ekledim). \u0130lgili kabiliyeti kullanmak i\u00e7in <em><strong>#include &lt;optional&gt;<\/strong><\/em> ba\u015fl\u0131k dosyas\u0131n\u0131 eklemeniz yeterli. <em>std::optional<\/em> ek olarak herhangi bir dinamik bellek kullan\u0131m\u0131 yapmaz, asl\u0131nda basit bir \u201cwrapper\u201d olarak da de\u011ferlendirilebilir. Genelde ilgili yap\u0131n\u0131n kendi kaplayaca\u011f\u0131 alan yan\u0131nda, fazladan sadece bir byte kadar bir alan kullan\u0131r.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Peki, hangi durumlarda bu yap\u0131y\u0131 kullanmal\u0131y\u0131z? G\u00fczel bir soru. Bu anlamda kalem kalem olas\u0131 kullan\u0131m alanlar\u0131ndan bahsetmeden \u00f6nce, boost::optional sayfas\u0131nda, <strong><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"https:\/\/www.boost.org\/doc\/libs\/1_70_0\/libs\/optional\/doc\/html\/boost_optional\/tutorial\/when_to_use_optional.html\">bu konuda verilen yaz\u0131ya<\/a> <\/span><\/strong>\u00a0<\/span><span style=\"font-weight: 400;\">bir g\u00f6z atabilirsiniz. \u0130lgili sayfada daha detayl\u0131 bilgi verilse de, k\u0131sa bir \u00f6zetini burada verelim:<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\"><em>optional&lt;<strong>T<\/strong>&gt;<\/em> yi ilgili <strong>T<\/strong> de\u011feri olmamas\u0131, <strong>T<\/strong>\u2019ye ili\u015fkin bir de\u011fer olmas\u0131 kadar normal, a\u00e7\u0131k ve \u00a0ge\u00e7erli bir sebebin oldu\u011fu durumlarda kullan\u0131lmas\u0131 tavsiye edilmektedir.<\/span><\/p><\/blockquote>\n<p><span style=\"font-weight: 400;\">Bu yap\u0131y\u0131 bir s\u0131n\u0131f \u00fcyesi olarak, d\u00f6n\u00fc\u015f de\u011feri olarak ya da fonksiyona ge\u00e7irilecek olan bir parametre olarak kullanabilirsiniz.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Detaylara ge\u00e7meden \u00f6nce hemen bir \u00f6rne\u011fe bakal\u0131m. Bu \u00f6rnek kod i\u00e7erisinde string&#8217;i int&#8217;e \u00e7eviren bir metoda bakaca\u011f\u0131z:<\/span><\/p>\n<pre class=\"lang:c++ decode:true \">#include &lt;optional&gt;\r\n#include &lt;string&gt;\r\n#include &lt;iostream&gt;\r\n\r\n\/\/ Ilgili string'i mumkunse int'e cevirelim \r\nstd::optional&lt;int&gt; convertToInt(const std::string&amp; s)\r\n{\r\n    try\r\n    {\r\n        return std::stoi(s);\r\n    }\r\n    catch (...) \r\n    {\r\n        return std::nullopt;\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    for (auto s : {\"42\", \" 077\", \"merhaba\", \"0x33\"} ) \r\n    {\r\n        \/\/ ilgili degerleri cevir bakalim\r\n\r\n        std::optional&lt;int&gt; oi = convertToInt(s);\r\n        \r\n        if (oi)\r\n        {\r\n            std::cout &lt;&lt; \"The integer obtained from '\" &lt;&lt; s &lt;&lt; \"' is: \" &lt;&lt; *oi &lt;&lt; '\\n';\r\n        }\r\n        else\r\n        {\r\n            std::cout &lt;&lt; \"The integer can not be obtained from '\" &lt;&lt; s &lt;&lt; \"'\\n\";\r\n        }\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">\u015eimdi kalem kalem <em>std::optional<\/em> kullan\u0131m\u0131na dair hususlar\u0131n \u00fczerinden, \u00f6rnek kodlar ile birlikte ge\u00e7elim isterseniz. Daha sonra bu kabiliyetleri i\u00e7eren daha kapsaml\u0131 kodlara da bakar\u0131z:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Herhangi bir zamanda <em>optional&lt;<strong>T<\/strong>&gt;<\/em> nesnesi ya bir de\u011fer i\u00e7erir ya da herhangi bir de\u011fer i\u00e7ermez ki bu da <em>std::nullopt<\/em> ile ifade edilir ki bu da asl\u0131nda <em>std::nullopt_t<\/em> tipindedir.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Nas\u0131l olu\u015fturabiliriz?<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Herhangi bir de\u011fer i\u00e7ermeyen nesneleri a\u015fa\u011f\u0131daki gibi olu\u015fturabiliriz. A\u015fa\u011f\u0131daki iki kullan\u0131mda bir de\u011fer i\u00e7ermez ve yap\u0131c\u0131 \u00e7a\u011fr\u0131lmaz<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">std::optional&lt;int&gt; inst1;\r\n\r\nstd::optional&lt;double&gt; inst2(std::nullopt);<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Bir de\u011fer veya farkl\u0131 bir nesne ile olu\u015fturmak i\u00e7in de a\u015fa\u011f\u0131daki kullan\u0131mlar ge\u00e7erlidir. Burada ayr\u0131ca tiplerden bahsetmeye gerek yok, std::optional bu \u00e7\u0131kar\u0131mlar\u0131 yapabilir:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">\/\/ Burada =&gt; optional&lt;int&gt;\r\nstd::optional intSample{42}; \r\n\r\nstd::optional&lt;std::string&gt; strSample{\"hello\"};\r\n\r\n\/\/ Burada =&gt; optional&lt;const char*&gt;\r\nstd::optional charArrSample{\"hello\"};<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Birden fazla parametre alan nesneler i\u00e7in ilgili nesneyi olu\u015fturup ge\u00e7irebilirsiniz. Ya da daha g\u00fczeli direk ilgili nesne ile birlikte olu\u015fturabilirsiniz, bu sayede ge\u00e7ici bir nesne olu\u015fturulmas\u0131ndan da kurtulursunuz. Bunun i\u00e7in de <em>std::in_place<\/em> yap\u0131s\u0131 kullan\u0131lmakta:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">std::optional complexSample { std::complex { 3.0, 4.0 } };\r\n\r\nstd::optional&lt;std::complex&lt;double&gt;&gt; inPlaceInstance {std::in_place, 3.0, 4.0};<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Ak\u0131ll\u0131 i\u015faret\u00e7ilerdeki gibi <em>std::make_optional<\/em> metodu da sunulmaktad\u0131r. Bu durumda, <em>std::in_place<\/em> kullanman\u0131za gerek yoktur:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">\/\/ optional&lt;double&gt;\r\nauto sampleOpt = std::make_optional(3.0); \r\nauto sampleOpt2 = std::make_optional&lt;std::complex&lt;double&gt;&gt;(3.0, 4.0);<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"> Yine konteynerler ile de kullan\u0131labilir:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">std::optional&lt;std::vector&lt;int&gt;&gt; vectorSample(std::in_place, {1, 2, 3, 4});<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">De\u011fere nas\u0131l ula\u015faca\u011f\u0131z?<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u0130lgili nesne i\u00e7erisindeki de\u011fere ise <em>value()<\/em> ile ula\u015fabilirsiniz. E\u011fer ilgili nesne herhangi bir de\u011fer i\u00e7ermiyor ise o zaman <em>std::bad_optional_access<\/em> istisnas\u0131 f\u0131rlat\u0131l\u0131r,<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Ak\u0131ll\u0131 i\u015faret\u00e7ilere benzer kullan\u0131m da sunulmaktad\u0131r. <strong>*<\/strong> ve <strong>-&gt;<\/strong> operat\u00f6rler tan\u0131mlanm\u0131\u015ft\u0131r ve bunlarda ilgili de\u011ferlere ula\u015fmak i\u00e7in kullan\u0131labilirler. Bir \u00f6nceki kullan\u0131m\u0131n aksine, e\u011fer ilgili nesne bir de\u011fer i\u00e7ermiyor ise, o zaman herhangi bir istisna f\u0131rlat\u0131lmaz ve davran\u0131\u015f tan\u0131ml\u0131 de\u011fildir. Yani dikkatli olmak laz\u0131m \ud83d\ude42<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">std::optional o{std::pair{42, \"hello\"}};\r\n\/\/ p pair&lt;int,string&gt; nesnesini tutar\r\nauto p = *o; \r\n\/\/ 42 bas\u0131l\u0131r\r\nstd::cout &lt;&lt; o-&gt;first; \r\n\r\nstd::optional&lt;std::string&gt; o{\"hello\"};\r\n\/\/ \u201dhello\u201d bas\u0131l\u0131r\r\nstd::cout &lt;&lt; *o;\r\n\/\/ \u201dhello\u201d bas\u0131l\u0131r\r\nstd::cout &lt;&lt; o.value(); \r\n\/\/ Nesneyi bosaltalim\r\no = std::nullopt;\r\n\r\n\/\/ Tan\u0131ml\u0131 olmayan davranis\r\nstd::cout &lt;&lt; *o;<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Son olarak <em>value_or(varsay\u0131lanDeger)<\/em> kullan\u0131m\u0131 da mevcut. Yani e\u011fer ilgili nesne herhangi bir de\u011fer i\u00e7ermiyor ise <em>varsay\u0131lanDeger<\/em> d\u00f6n\u00fcl\u00fcr.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">\/\/ Bos bir nesne olusturalim\r\nstd::optional&lt;double&gt; odouble;\r\n\r\n\/\/ Bos oldugu icin varsayilan olarak ifade edilen deger donulur\r\nstd::cout&lt;&lt; \"odouble \" &lt;&lt; odouble.value_or(10.0) &lt;&lt; '\\n';<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">De\u011fer var m\u0131 nas\u0131l sorgulayaca\u011f\u0131z?<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Herhangi bir <em>std::optional<\/em> nesnesinin bir veri i\u00e7erip\/i\u00e7ermedi\u011fini <em>has_value()<\/em> metodu ile sorgulayabilirsiniz.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Ayr\u0131ca <\/span><em><span style=\"font-weight: 400;\">operator <\/span><span style=\"font-weight: 400;\">bool<\/span><span style=\"font-weight: 400;\">()<\/span><\/em><span style=\"font-weight: 400;\">\u00a0da tan\u0131ml\u0131 oldu\u011fu i\u00e7in, nesnesinin kendisini <em>if blo\u011funda<\/em> kontrol edebilirsiniz.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">A\u015fa\u011f\u0131da bu kullan\u0131mlar\u0131 g\u00f6rebilirsiniz:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">\/\/ std::optional&lt;int&gt;\r\nstd::optional o{15};\r\n\r\n\/\/ true\r\nif (o)  \r\n  ...\r\n\/\/ false\r\nif (!o)\r\n  ... \r\n\r\n\/\/ true\r\nif (o.has_value()) \r\n  ...<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Mevcut de\u011feri nas\u0131l de\u011fi\u015ftirebiliriz?<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Atama (<strong>=<\/strong>) operat\u00f6r\u00fc ve <em>emplace()<\/em> API si ile bunu ger\u00e7ekle\u015ftirebiliriz:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">\/\/ o da henuz bir deger yok\r\nstd::optional&lt;std::complex&lt;double&gt;&gt; inst;\r\n\r\n\/\/ optimal&lt;int&gt; tipi\r\nstd::optional inst2{77};\r\n\r\ninst = 42;\r\n\r\n\/\/ ilgili nesne tipi std::complex&lt;double&gt; olarak degisiyor\r\ninst = {9.9, 4.4}; \/\/ value becomes complex(9.9, 4.4)\r\n\r\n\/\/ Tip yine degisiyor :)\r\ninst = inst2; \r\n\r\n\/\/ Artik bir deger yok\r\ninst = std::nullopt;\r\n\r\n\/\/ Artik var\r\ninst.emplace(5.5, 7.7);\r\n\r\n\/\/ Yine yok\r\ninst = {};<\/pre>\n<ul>\n<li style=\"font-weight: 400; list-style-type: none;\">\n<ul>\n<li><span style=\"font-weight: 400;\">Ayr\u0131ca i\u015faret\u00e7ilerdeki gibi <strong>*<\/strong> operat\u00f6r\u00fc ile de ilgili nesnelere de\u011fer atanabilir:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">std::optional&lt;std::complex&lt;double&gt;&gt; inst;\r\n\r\n\/\/ Hatali kullanim. Tanimli olmayan davranis\r\n*inst = 42; \r\n\r\n\/\/ Sikinti yok\r\ninst.emplace(2.2, 3.3);\r\n\r\n...\r\n\r\nif (inst) \r\n{\r\n    *inst = 88;\r\n    *inst = {1.2, 3.4};\r\n}<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Atama yan\u0131nda <em>std::optional&lt;&gt;<\/em> ayn\u0131 zamanda ta\u015f\u0131ma mant\u0131klar\u0131n\u0131 da desteklemektedir. Bir di\u011fer ifade ile <em>std::move<\/em> ile farkl\u0131 bir nesne, bir di\u011fer nesneye ta\u015f\u0131nabilir.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Kar\u015f\u0131la\u015ft\u0131rma operat\u00f6rleri ile kullanabilir miyiz?<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Standart kar\u015f\u0131la\u015ft\u0131rma operat\u00f6rlerini nesneler ile kullanabilirsiniz.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">std::optional&lt;int&gt; empty;\r\nstd::optional&lt;int&gt; two(2);\r\nstd::optional&lt;int&gt; four(4);\r\n\r\n\/\/ false \/ true basmak i\u00e7in\r\nstd::cout &lt;&lt; std::boolalpha; \r\n\r\n\/\/ true (four &gt; two)\r\nstd::cout &lt;&lt; (four &gt; two) &lt;&lt; \"\\n\";\r\n\r\n\/\/ false (four &lt; two)\r\nstd::cout &lt;&lt; (four &lt; two) &lt;&lt; \"\\n\";\r\n\r\n\/\/ false (four == two)\r\nstd::cout &lt;&lt; (four == 4) &lt;&lt; \"\\n\";<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">E\u011fer her iki nesne i\u00e7erisinde de de\u011fer yok ise <strong>==<\/strong> true d\u00f6ner. Di\u011fer kar\u015f\u0131la\u015ft\u0131rmalar\u0131n hepsi false d\u00f6ner. Yaln\u0131z, nesnelerden birisi e\u011fer <em>std::nullopt<\/em> ise yani yok ise farkl\u0131 durumlar ortaya \u00e7\u0131kabilir. \u0130\u00e7erisinde de\u011fer olmayan nesne her zaman di\u011ferlerinden az olarak kabul edilir:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">std::optional&lt;int&gt; empty;\r\nstd::optional&lt;int&gt; two(2);\r\n\r\n\/\/ true\r\nempty == std::nullopt\r\n\r\n\/\/ false\r\nempty== 2\r\n\r\n\/\/ true\r\nempty &lt; two\r\n\r\n\/\/ false\r\nempty &gt; two\r\n\r\n\/\/ true\r\ntwo == 2\r\n\r\n\/\/ true\r\nempty &lt; two<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><em>std::optional<\/em> metot d\u00f6n\u00fc\u015f de\u011feri olarak nas\u0131l kullan\u0131l\u0131r? Yaz\u0131n\u0131n ba\u015f\u0131nda verdi\u011fimiz kod par\u00e7as\u0131 bu anlamda asl\u0131nda g\u00fczel bir \u00f6rnek.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><em>reset()<\/em> metodu ile de nesne i\u00e7erisindeki tutulan de\u011fer at\u0131l\u0131r ve bo\u015f hale getirilir.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">\u015eimdi \u00e7ok temel baz\u0131 kullan\u0131mlar\u0131 i\u00e7eren \u00f6rnek kodlara bakal\u0131m. Bunlar\u0131n ilki, referanslarda verdi\u011fim bir kitaptan ald\u0131\u011f\u0131m kod:<\/span><\/p>\n<pre class=\"lang:c++ decode:true \">#include &lt;optional&gt;\r\n#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nclass Name\r\n{\r\nprivate:\r\n\tstring mFirst;\r\n\r\n\t\/\/ optional'in uye olarak kullanilmasi\r\n\toptional&lt;string&gt; mMiddle;\r\n\r\n\tstring mLast;\r\npublic:\r\n\t\/\/ optional'in parametre olarak gecirilmesi ve move semantics icin ornek kullanim\r\n\tName(string f,\r\n\t\toptional&lt;string&gt; m,\r\n\t\tstring l)\r\n\t\t: mFirst{ move(f) }, mMiddle{ move(m) }, mLast{ move(l) } \r\n\t{\r\n\t}\r\n\r\n\tfriend ostream&amp; operator &lt;&lt; (ostream&amp; strm, const Name&amp; n);\r\n\t{\r\n\t\tstrm &lt;&lt; n.mFirst &lt;&lt; ' ';\r\n\r\n\t\tif (n.mMiddle)\r\n\t\t{\r\n\t\t\tstrm &lt;&lt; *n.mMiddle &lt;&lt; ' ';\r\n\t\t}\r\n\r\n\t\treturn strm &lt;&lt; n.mLast;\r\n\t}\r\n};\r\n\r\nint main()\r\n{\r\n\t\/\/ Gobek ismi yok\r\n\tName n { \"Ayse\", nullopt, \"Kopar\" };\r\n\tcout &lt;&lt; n &lt;&lt; '\\n';\r\n\r\n\t\/\/ Gobek ismi var\r\n\tName m{ \"Ahmet\", \"Ekrem\", \"Uzar\" };\r\n\tcout &lt;&lt; m &lt;&lt; '\\n';\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">D<\/span><span style=\"font-weight: 400;\">i\u011feri de ilgili referans sayfas\u0131ndan. Bu iki \u00f6rnek de sizlere <em>std::optional<\/em> kullan\u0131m\u0131 hakk\u0131nda sizlere fikir verecektir.\u00a0<\/span><\/p>\n<pre class=\"lang:c++ decode:true \">#include &lt;string&gt;\r\n#include &lt;functional&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;optional&gt;\r\n \r\n\/\/ Factory benzeri yapilardan donus icin kullanima ornek\r\nstd::optional&lt;std::string&gt; create(bool b) \r\n{\r\n    if (b)\r\n        return \"Godzilla\";\r\n    return {};\r\n}\r\n \r\n\/\/ auto donus degeri ile kullanima ornek\r\nauto create2(bool b) \r\n{\r\n    return b ? std::optional&lt;std::string&gt;{\"Godzilla\"} : std::nullopt;\r\n}\r\n \r\n\/\/ referans tipinde donus saglamak icin std::reference_wrapper kullanilabilir\r\nauto create_ref(bool b) \r\n{\r\n    static std::string value = \"Godzilla\";\r\n\r\n    return b ? std::optional&lt;std::reference_wrapper&lt;std::string&gt;&gt;{value}\r\n             : std::nullopt;\r\n}\r\n \r\nint main()\r\n{\r\n    std::cout &lt;&lt; \"create(false) returned \"\r\n              &lt;&lt; create(false).value_or(\"empty\") &lt;&lt; '\\n';\r\n \r\n    \/\/ Yukaridaki metotlar ile donulen degerleri kosul kontrolleri icin kullanabiliriz\r\n    if (auto str = create2(true); str) \r\n    {\r\n        std::cout &lt;&lt; \"create2(true) returned \" &lt;&lt; *str &lt;&lt; '\\n';\r\n    }\r\n \r\n    if (auto str = create_ref(true); str) \r\n    {\r\n        \/\/ get() API si ile reference_wrapper tarafindan tutulan degere erisim\r\n        std::cout &lt;&lt; \"create_ref(true) returned \" &lt;&lt; str-&gt;get() &lt;&lt; '\\n';\r\n        str-&gt;get() = \"Mothra\";\r\n        std::cout &lt;&lt; \"modifying it changed it to \" &lt;&lt; str-&gt;get() &lt;&lt; '\\n';\r\n    }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Bir sonraki yaz\u0131mda g\u00f6r\u00fc\u015fmek dile\u011fiyle. Bol kodlu g\u00fcnler:<\/span><\/p>\n<h2><strong><span style=\"color: #0000ff;\">Kaynaklar:<\/span><\/strong><\/h2>\n<p><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"https:\/\/www.boost.org\/doc\/libs\/1_70_0\/libs\/optional\/doc\/html\/boost_optional\/tutorial\/when_to_use_optional.html\"><strong>https:\/\/www.boost.org\/doc\/libs\/1_70_0\/libs\/optional\/doc\/html\/boost_optional\/tutorial\/when_to_use_optional.html<\/strong><\/a><\/span><\/p>\n<p><span style=\"color: #008000;\"><strong><a style=\"color: #008000;\" href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/optional\">https:\/\/en.cppreference.com\/w\/cpp\/utility\/optional<\/a><\/strong><\/span><\/p>\n<p><span style=\"color: #008000;\"><strong><a style=\"color: #008000;\" href=\"https:\/\/arne-mertz.de\/2018\/06\/modern-c-features-stdoptional\/\">https:\/\/arne-mertz.de\/2018\/06\/modern-c-features-stdoptional\/<\/a><\/strong><\/span><\/p>\n<p><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"http:\/\/www.cppstd17.com\/\"><strong>C++ 17 The Complete Guide, Nicolai M. Josuttis<\/strong><\/a><\/span><\/p>\n<p><span style=\"color: #008000;\"><strong><a style=\"color: #008000;\" href=\"https:\/\/www.fluentcpp.com\/2016\/11\/24\/clearer-interfaces-with-optionalt\/\">https:\/\/www.fluentcpp.com\/2016\/11\/24\/clearer-interfaces-with-optionalt\/<\/a><\/strong><\/span>[:en]Hello, my friends, we are together with another weekly C++ post. The topic of this post is about std::optional which is introduced with C++ 17. In this post, we are going to take alook at why we need such capability an in which cases we can use that with example codes. This post will be followed by std::variant and std::any topics which are also related with this topic.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/images.unsplash.com\/photo-1493612276216-ee3925520721?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1000&amp;q=80\" alt=\"person holding light bulb\" width=\"430\" height=\"537\" \/><\/p>\n<p>First of all, let us look at why we need such feature. I am sure, at one point in your code, you needed to state whether a given variable contains value or not (i.e. middle names or optional arguments provided via command line). Of course, there are solutions that can be used to deal with this problem. For instance, for pointers, you may use\u00a0<em>nullptr<\/em> to state but it still does not give much information about whether given pointer is not allocated or does not contain any valid value, right? This is a little bit different for other types. -1 or similar values can be used for integral values, but in that case, -1 can not be used\u00a0 anymore. It may also not be possible to come up with such numbers or not suitable (i.e. for floating numbers). Besides, in those cases, you usually required to perform additional checks.<\/p>\n<p><em>std::optional<\/em>, is introduced to resolve such issues. With this feature, we are able to check if a value is available or not in a more standard and expressive manner. Such types are also known as <code>nullable types<\/code>in some references. Similarly, this can be used for returning values and passing parameters to functions which may or may not be available<span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Most of the <em>std::optional <\/em>features are based on <em>boost::optional,\u00a0<\/em>so if you are familiar with that library, you can easily adapt std::optional in your codes (by the way I have added an example reference for boost::optional usage). To use std::optional, you need to add\u00a0<em><strong>#include &lt;optional&gt;<\/strong><\/em>\u00a0header file. <em>std::optional<\/em>\u00a0does not allocate memory from heap, in fact, it is simply a wrapper around the type provided. At most, one additional byte is allocated to specify availability.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So, afterall, in which cases we should use this feature? Good question. Let us look at a part of explanation given in boost::optional page, to see the full description you can check out <strong><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"https:\/\/www.boost.org\/doc\/libs\/1_70_0\/libs\/optional\/doc\/html\/boost_optional\/tutorial\/when_to_use_optional.html\">this page<\/a><\/span><\/strong><\/span><span style=\"font-weight: 400;\">.:<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">`It is recommended to use\u00a0<code>optional&lt;T&gt;<\/code>\u00a0in situations where there is exactly one, clear (to all parties) reason for having no value of type\u00a0<code>T<\/code>, and where the lack of value is as natural as having any regular value of\u00a0<code>T<\/code>.`<\/span><\/p><\/blockquote>\n<p><span style=\"font-weight: 400;\">You can use std::optional as class member, return value or parameter that will be passed to functions. Before delving into details and other examples, let us look at a simple example code that uses std::optional for a function that convert (if possible) string to integer<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;optional&gt;\r\n#include &lt;string&gt;\r\n#include &lt;iostream&gt;\r\n\r\n\/\/ Convert string to int if possible \r\nstd::optional&lt;int&gt; convertToInt(const std::string&amp; s)\r\n{\r\n    try\r\n    {\r\n        return std::stoi(s);\r\n    }\r\n    catch (...) \r\n    {\r\n        return std::nullopt;\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    for (auto s : {\"42\", \" 077\", \"merhaba\", \"0x33\"} ) \r\n    {\r\n        \/\/ Convert the string to int\r\n        std::optional&lt;int&gt; oi = convertToInt(s);\r\n        \r\n        if (oi)\r\n        {\r\n            std::cout &lt;&lt; \"The integer obtained from '\" &lt;&lt; s &lt;&lt; \"' is: \" &lt;&lt; *oi &lt;&lt; '\\n';\r\n        }\r\n        else\r\n        {\r\n            std::cout &lt;&lt; \"The integer can not be obtained from '\" &lt;&lt; s &lt;&lt; \"'\\n\";\r\n        }\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Now, let us go over individual capabilities of <em>std::optional<\/em>\u00a0usage one by one with example source code snippets. Later, we will look at complete code examples:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">At any time, an instance of <em>optional&lt;<strong>T<\/strong>&gt;<\/em>\u00a0may either contain a T value or does not contain any value at all which is express with\u00a0<em>std::nullopt<\/em> (type of\u00a0<em>std::nullopt_t)<\/em>.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">How can we create an instance of optional&lt;T&gt;?<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">We can create <em>std::optional<\/em>\u00a0instances that do not contain any values as follows<\/span><span style=\"font-weight: 400;\">. Both of following instances do not contain any value and no contructor is called:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">std::optional&lt;int&gt; inst1;\r\n\r\nstd::optional&lt;double&gt; inst2(std::nullopt);<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">We can create <em>std::optional<\/em>\u00a0instances with given values or instances as follow. std::optional can also deduce types from provided parameters:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">\/\/ Here instance is =&gt; optional&lt;int&gt;\r\nstd::optional intSample{42}; \r\n\r\nstd::optional&lt;std::string&gt; strSample{\"hello\"};\r\n\r\n\/\/ Here instance is =&gt; optional&lt;const char*&gt;\r\nstd::optional charArrSample{\"hello\"};<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The instances that require multiple parameters, can be separately created and passed to <em>std::optional<\/em> instances which may cause temporary copy operations. The\u00a0<em>std::optional<\/em> instances can directly be created with given multiple parameters (like emplace_back in STL containers) which prevent temporary copy operations. <em>std::in_place<\/em>\u00a0is used for this purpose:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">std::optional complexSample { std::complex { 3.0, 4.0 } };\r\n\r\nstd::optional&lt;std::complex&lt;double&gt;&gt; inPlaceInstance {std::in_place, 3.0, 4.0};<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><em>std::make_optional<\/em>\u00a0method can also be used for instantiation of <em>std::optional<\/em>\u00a0objects like in smart pointers. In this case, <em>std::in_place<\/em>\u00a0is not required:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">\/\/ optional&lt;double&gt;\r\nauto sampleOpt = std::make_optional(3.0); \r\nauto sampleOpt2 = std::make_optional&lt;std::complex&lt;double&gt;&gt;(3.0, 4.0);<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">std::optional can also be used with STL containers:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">std::optional&lt;std::vector&lt;int&gt;&gt; vectorSample(std::in_place, {1, 2, 3, 4});<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">How can we access the contained values?<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><em>value()\u00a0<\/em>API can be used to access the value contained in <em>std::optional<\/em> instance. If given instance does not contain any value, then <em>std::bad_optional_access<\/em>\u00a0exception is thrown,<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">In addition to <em>value() <\/em>API, pointer operators such as <strong>*<\/strong> and <strong>-&gt;<\/strong>\u00a0can also be used with <em>std::optional<\/em> instances. Contrary to previous usage, if instance does not contain any value, then no exception is thrown and behavior is undefined. So you have to be careful \ud83d\ude42<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">std::optional o{std::pair{42, \"hello\"}};\r\n\r\n\/\/ p contains pair&lt;int,string&gt; instance\r\nauto p = *o; \r\n\r\n\/\/ 42 is printed\r\nstd::cout &lt;&lt; o-&gt;first; \r\n\r\nstd::optional&lt;std::string&gt; o{\"hello\"};\r\n\r\n\/\/ \u201dhello\u201d is printed\r\nstd::cout &lt;&lt; *o;\r\n\r\n\/\/ \u201dhello\u201d is printed\r\nstd::cout &lt;&lt; o.value(); \r\n\r\n\/\/ Clear the instance\r\no = std::nullopt;\r\n\r\n\/\/ Undefined behavior!\r\nstd::cout &lt;&lt; *o;<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Lastly, <em>value_or(defaultValue)<\/em>\u00a0API is also provided with optional library which returns defaultValue if given <em>std::optional <\/em>instance does not contain any value.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">\/\/ Create an empty instance\r\nstd::optional&lt;double&gt; odouble;\r\n\r\n\/\/ Here 10.0, the returned default value, is printed\r\nstd::cout&lt;&lt; \"odouble \" &lt;&lt; odouble.value_or(10.0) &lt;&lt; '\\n';<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">How can we check if given <em>std::optional<\/em> instance contain any value?<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><em>has_value()<\/em>\u00a0API can be used to query if given instance contain any value,<\/span><\/li>\n<li style=\"font-weight: 400;\"><em><span style=\"font-weight: 400;\">operator <\/span><span style=\"font-weight: 400;\">bool<\/span><span style=\"font-weight: 400;\">()<\/span><\/em><span style=\"font-weight: 400;\">\u00a0is also defined and can be used with conditional statements such as <em>if-else if,<\/em><\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Example usages are provided below:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true \">\/\/ std::optional&lt;int&gt;\r\nstd::optional o{15};\r\n\r\n\/\/ true\r\nif (o)  \r\n  ...\r\n\/\/ false\r\nif (!o)\r\n  ... \r\n\r\n\/\/ true\r\nif (o.has_value()) \r\n  ...<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">How can we update existing value?<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Assignment operator (<strong>=<\/strong>) ve <em>emplace()<\/em> API can be used for this purpose:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">\/\/ No value is available for this instance\r\nstd::optional&lt;std::complex&lt;double&gt;&gt; inst;\r\n\r\n\/\/ optimal&lt;int&gt; type\r\nstd::optional inst2{77};\r\n\r\ninst = 42;\r\n\r\n\/\/ the type contained in std::optional is updated as std::complex&lt;double&gt;\r\ninst = {9.9, 4.4}; \r\n\r\n\/\/ Type is chnaged again :)\r\ninst = inst2; \r\n\r\n\/\/ No more value is contained\r\ninst = std::nullopt;\r\n\r\n\/\/ Now it contains\r\ninst.emplace(5.5, 7.7);\r\n\r\n\/\/ Not anymore :)\r\ninst = {};<\/pre>\n<ul>\n<li style=\"font-weight: 400; list-style-type: none;\">\n<ul>\n<li><span style=\"font-weight: 400;\">* operator can also be used for assignment, but the given std::optional instance should contain a value:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">std::optional&lt;std::complex&lt;double&gt;&gt; inst;\r\n\r\n\/\/ Incorect usage. Undefined behavior\r\n*inst = 42; \r\n\r\n\/\/ No problem\r\ninst.emplace(2.2, 3.3);\r\n\r\n...\r\n\r\nif (inst) \r\n{\r\n    *inst = 88;\r\n    *inst = {1.2, 3.4};\r\n}<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><em>std::optional&lt;&gt;<\/em>\u00a0also support <em>move semantics<\/em>. In other words, <em>std::move<\/em>\u00a0can be used to transfer instances from other instances.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Can we use comparison operators?<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">You can use standard comparison operators with <em>std::optional\u00a0<\/em>instances.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">std::optional&lt;int&gt; empty;\r\nstd::optional&lt;int&gt; two(2);\r\nstd::optional&lt;int&gt; four(4);\r\n\r\n\/\/ To print false \/ true \r\nstd::cout &lt;&lt; std::boolalpha; \r\n\r\n\/\/ true (four &gt; two)\r\nstd::cout &lt;&lt; (four &gt; two) &lt;&lt; \"\\n\";\r\n\r\n\/\/ false (four &lt; two)\r\nstd::cout &lt;&lt; (four &lt; two) &lt;&lt; \"\\n\";\r\n\r\n\/\/ false (four == two)\r\nstd::cout &lt;&lt; (four == 4) &lt;&lt; \"\\n\";<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If both of instances do no contain any value,\u00a0<strong>==<\/strong>\u00a0comparison returns true. If one instance does not contain any value, then different results can be observed such that no value instance always considered less than other instance that contain value. Let us look at example usages<\/span><span style=\"font-weight: 400;\">:<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">std::optional&lt;int&gt; empty;\r\nstd::optional&lt;int&gt; two(2);\r\n\r\n\/\/ true\r\nempty == std::nullopt\r\n\r\n\/\/ false\r\nempty== 2\r\n\r\n\/\/ true\r\nempty &lt; two\r\n\r\n\/\/ false\r\nempty &gt; two\r\n\r\n\/\/ true\r\ntwo == 2\r\n\r\n\/\/ true\r\nempty &lt; two<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">How\u00a0<em>std::optional<\/em>\u00a0can be used for function returns is given in the sample code that is provided at the beginning of my post.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><em>reset()<\/em>\u00a0API can be used to reset\/clear the value contained in given instance.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">It is time to look at two complete code examples which illustrate basic std::optional usages. I took first example from the book that I gave in references section:<\/span><\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;optional&gt;\r\n#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nclass Name\r\n{\r\nprivate:\r\n\tstring mFirst;\r\n\r\n\t\/\/ Example use of optional as class member\r\n\toptional&lt;string&gt; mMiddle;\r\n\r\n\tstring mLast;\r\npublic:\r\n\t\/\/ Example use of optional as function parameter and move semantics\r\n\tName(string f,\r\n\t\toptional&lt;string&gt; m,\r\n\t\tstring l)\r\n\t\t: mFirst{ move(f) }, mMiddle{ move(m) }, mLast{ move(l) } \r\n\t{\r\n\t}\r\n\r\n\tfriend ostream&amp; operator &lt;&lt; (ostream&amp; strm, const Name&amp; n);\r\n\t{\r\n\t\tstrm &lt;&lt; n.mFirst &lt;&lt; ' ';\r\n\r\n\t\tif (n.mMiddle)\r\n\t\t{\r\n\t\t\tstrm &lt;&lt; *n.mMiddle &lt;&lt; ' ';\r\n\t\t}\r\n\r\n\t\treturn strm &lt;&lt; n.mLast;\r\n\t}\r\n};\r\n\r\nint main()\r\n{\r\n\t\/\/ No middle name\r\n\tName n { \"Ayse\", nullopt, \"Kopar\" };\r\n\tcout &lt;&lt; n &lt;&lt; '\\n';\r\n\r\n\t\/\/ Have middle name\r\n\tName m{ \"Ahmet\", \"Ekrem\", \"Uzar\" };\r\n\tcout &lt;&lt; m &lt;&lt; '\\n';\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">The other example code is taken from CPP reference page<\/span><span style=\"font-weight: 400;\">. I hope these two example codes help you to understand <em>std::optional<\/em> better.\u00a0<\/span><\/p>\n<pre class=\"lang:c++ decode:true \">#include &lt;string&gt;\r\n#include &lt;functional&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;optional&gt;\r\n \r\n\/\/ Example usage for factory like methods\r\nstd::optional&lt;std::string&gt; create(bool b) \r\n{\r\n    if (b)\r\n        return \"Godzilla\";\r\n    return {};\r\n}\r\n \r\n\/\/ returning as auto values\r\nauto create2(bool b) \r\n{\r\n    return b ? std::optional&lt;std::string&gt;{\"Godzilla\"} : std::nullopt;\r\n}\r\n \r\n\/\/ returning as reference type via std::reference_wrapper usage\r\nauto create_ref(bool b) \r\n{\r\n    static std::string value = \"Godzilla\";\r\n\r\n    return b ? std::optional&lt;std::reference_wrapper&lt;std::string&gt;&gt;{value}\r\n             : std::nullopt;\r\n}\r\n \r\nint main()\r\n{\r\n    std::cout &lt;&lt; \"create(false) returned \"\r\n              &lt;&lt; create(false).value_or(\"empty\") &lt;&lt; '\\n';\r\n \r\n    \/\/ The returned std::optional instance can be used for conditional statements\r\n    if (auto str = create2(true); str) \r\n    {\r\n        std::cout &lt;&lt; \"create2(true) returned \" &lt;&lt; *str &lt;&lt; '\\n';\r\n    }\r\n \r\n    if (auto str = create_ref(true); str) \r\n    {\r\n        \/\/ get() API can be used to access the value reside at reference_wrapper\r\n        std::cout &lt;&lt; \"create_ref(true) returned \" &lt;&lt; str-&gt;get() &lt;&lt; '\\n';\r\n        str-&gt;get() = \"Mothra\";\r\n        std::cout &lt;&lt; \"modifying it changed it to \" &lt;&lt; str-&gt;get() &lt;&lt; '\\n';\r\n    }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">See you in my next post. Have plenty of coding \ud83d\ude42<\/span><\/p>\n<h2><strong><span style=\"color: #0000ff;\">References:<\/span><\/strong><\/h2>\n<p><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"https:\/\/www.boost.org\/doc\/libs\/1_70_0\/libs\/optional\/doc\/html\/boost_optional\/tutorial\/when_to_use_optional.html\"><strong>https:\/\/www.boost.org\/doc\/libs\/1_70_0\/libs\/optional\/doc\/html\/boost_optional\/tutorial\/when_to_use_optional.html<\/strong><\/a><\/span><\/p>\n<p><span style=\"color: #008000;\"><strong><a style=\"color: #008000;\" href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/optional\">https:\/\/en.cppreference.com\/w\/cpp\/utility\/optional<\/a><\/strong><\/span><\/p>\n<p><span style=\"color: #008000;\"><strong><a style=\"color: #008000;\" href=\"https:\/\/arne-mertz.de\/2018\/06\/modern-c-features-stdoptional\/\">https:\/\/arne-mertz.de\/2018\/06\/modern-c-features-stdoptional\/<\/a><\/strong><\/span><\/p>\n<p><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"http:\/\/www.cppstd17.com\/\"><strong>C++ 17 The Complete Guide, Nicolai M. Josuttis<\/strong><\/a><\/span><\/p>\n<p><span style=\"color: #008000;\"><strong><a style=\"color: #008000;\" href=\"https:\/\/www.fluentcpp.com\/2016\/11\/24\/clearer-interfaces-with-optionalt\/\">https:\/\/www.fluentcpp.com\/2016\/11\/24\/clearer-interfaces-with-optionalt\/<\/a><\/strong><\/span>[:]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[:tr]Merhabalar arkada\u015flar, yeni bir haftal\u0131k C++ yaz\u0131m\u0131z ile birlikteyiz. Bu yaz\u0131m\u0131n konusu, C++ 17 ile birlikte dile dahil edilen std::optional yetene\u011fi. Bu kabiliyete neden ihtiyac\u0131m\u0131z var, nerelerde kullanabiliriz gibi sorulara \u00e7e\u015fitli kod \u00f6rnekleri \u00fczerinden giderek bakaca\u011f\u0131z. Bu yap\u0131 ile ilintili olarak std::variant ve std::any yap\u0131lar\u0131na da farkl\u0131 yaz\u0131lar\u0131mda de\u011finece\u011fim. O zaman hemen ba\u015flayal\u0131m ne dersiniz&#8230;. <a class=\"more-link\" href=\"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/\">Continue reading <span class=\"meta-nav\">&#8594;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":174,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"dsgo_overlay_header":false,"dsgo_overlay_header_text_color":"","dsgo_overlay_skip_top_bar":false,"_designsetgo_exclude_llms":false,"footnotes":""},"categories":[10,41],"tags":[404,234,407,410,42,411,408,406,105,403,405,409,236],"class_list":["post-1107","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","category-modern-c","tag-boostoptional","tag-c-17","tag-has_value","tag-in_place","tag-modern-c","tag-nullable-types","tag-nullopt","tag-stdany","tag-stdmove","tag-stdoptional","tag-stdvariant","tag-value_or","tag-weekly-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[:tr]Haftal\u0131k C++ 14 \u2013 std::optional[:en]Weekly C++ 14 \u2013 std::optional[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[:tr]Haftal\u0131k C++ 14 \u2013 std::optional[:en]Weekly C++ 14 \u2013 std::optional[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131\" \/>\n<meta property=\"og:description\" content=\"[:tr]Merhabalar arkada\u015flar, yeni bir haftal\u0131k C++ yaz\u0131m\u0131z ile birlikteyiz. Bu yaz\u0131m\u0131n konusu, C++ 17 ile birlikte dile dahil edilen std::optional yetene\u011fi. Bu kabiliyete neden ihtiyac\u0131m\u0131z var, nerelerde kullanabiliriz gibi sorulara \u00e7e\u015fitli kod \u00f6rnekleri \u00fczerinden giderek bakaca\u011f\u0131z. Bu yap\u0131 ile ilintili olarak std::variant ve std::any yap\u0131lar\u0131na da farkl\u0131 yaz\u0131lar\u0131mda de\u011finece\u011fim. O zaman hemen ba\u015flayal\u0131m ne dersiniz.... Continue reading &#8594;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/\" \/>\n<meta property=\"og:site_name\" content=\"Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-15T18:29:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-18T20:22:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png\" \/>\n\t<meta property=\"og:image:width\" content=\"32\" \/>\n\t<meta property=\"og:image:height\" content=\"32\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"yaz\u0131l\u0131mperver\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Yazan:\" \/>\n\t<meta name=\"twitter:data1\" content=\"yaz\u0131l\u0131mperver\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data2\" content=\"20 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/\"},\"author\":{\"name\":\"yaz\u0131l\u0131mperver\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#\\\/schema\\\/person\\\/534a27860d56170658018ac4c6f1af16\"},\"headline\":\"[:tr]Haftal\u0131k C++ 14 \u2013 std::optional[:en]Weekly C++ 14 \u2013 std::optional[:]\",\"datePublished\":\"2019-04-15T18:29:53+00:00\",\"dateModified\":\"2019-04-18T20:22:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/\"},\"wordCount\":2419,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#\\\/schema\\\/person\\\/534a27860d56170658018ac4c6f1af16\"},\"image\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/FeaturedImage-e1580417052514.png\",\"keywords\":[\"boost::optional\",\"c++ 17\",\"has_value\",\"in_place\",\"Modern C++\",\"nullable types\",\"nullopt\",\"std::any\",\"std::move\",\"std::optional\",\"std::variant\",\"value_or\",\"weekly c++\"],\"articleSection\":[\"C++\",\"Modern C++\"],\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/\",\"url\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/\",\"name\":\"[:tr]Haftal\u0131k C++ 14 \u2013 std::optional[:en]Weekly C++ 14 \u2013 std::optional[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/FeaturedImage-e1580417052514.png\",\"datePublished\":\"2019-04-15T18:29:53+00:00\",\"dateModified\":\"2019-04-18T20:22:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"tr\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/#primaryimage\",\"url\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/FeaturedImage-e1580417052514.png\",\"contentUrl\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/FeaturedImage-e1580417052514.png\",\"width\":32,\"height\":32},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2019\\\/04\\\/15\\\/haftalik-c-14-stdoptional\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\\\/\\\/yazilimperver.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Haftal\u0131k C++ 14 \u2013 std::optional\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#website\",\"url\":\"https:\\\/\\\/yazilimperver.net\\\/\",\"name\":\"Yaz\u0131l\u0131mperver'in D\u00fcnyas\u0131\",\"description\":\"Payla\u015fmak g\u00fczeldir.\",\"publisher\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#\\\/schema\\\/person\\\/534a27860d56170658018ac4c6f1af16\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/yazilimperver.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"tr\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#\\\/schema\\\/person\\\/534a27860d56170658018ac4c6f1af16\",\"name\":\"yaz\u0131l\u0131mperver\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"tr\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/cropped-OnlyIcon-1.png\",\"url\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/cropped-OnlyIcon-1.png\",\"contentUrl\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/cropped-OnlyIcon-1.png\",\"width\":512,\"height\":512,\"caption\":\"yaz\u0131l\u0131mperver\"},\"logo\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/cropped-OnlyIcon-1.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[:tr]Haftal\u0131k C++ 14 \u2013 std::optional[:en]Weekly C++ 14 \u2013 std::optional[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/","og_locale":"tr_TR","og_type":"article","og_title":"[:tr]Haftal\u0131k C++ 14 \u2013 std::optional[:en]Weekly C++ 14 \u2013 std::optional[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131","og_description":"[:tr]Merhabalar arkada\u015flar, yeni bir haftal\u0131k C++ yaz\u0131m\u0131z ile birlikteyiz. Bu yaz\u0131m\u0131n konusu, C++ 17 ile birlikte dile dahil edilen std::optional yetene\u011fi. Bu kabiliyete neden ihtiyac\u0131m\u0131z var, nerelerde kullanabiliriz gibi sorulara \u00e7e\u015fitli kod \u00f6rnekleri \u00fczerinden giderek bakaca\u011f\u0131z. Bu yap\u0131 ile ilintili olarak std::variant ve std::any yap\u0131lar\u0131na da farkl\u0131 yaz\u0131lar\u0131mda de\u011finece\u011fim. O zaman hemen ba\u015flayal\u0131m ne dersiniz.... Continue reading &#8594;","og_url":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/","og_site_name":"Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131","article_published_time":"2019-04-15T18:29:53+00:00","article_modified_time":"2019-04-18T20:22:52+00:00","og_image":[{"width":32,"height":32,"url":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png","type":"image\/png"}],"author":"yaz\u0131l\u0131mperver","twitter_card":"summary_large_image","twitter_misc":{"Yazan:":"yaz\u0131l\u0131mperver","Tahmini okuma s\u00fcresi":"20 dakika"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/#article","isPartOf":{"@id":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/"},"author":{"name":"yaz\u0131l\u0131mperver","@id":"https:\/\/yazilimperver.net\/#\/schema\/person\/534a27860d56170658018ac4c6f1af16"},"headline":"[:tr]Haftal\u0131k C++ 14 \u2013 std::optional[:en]Weekly C++ 14 \u2013 std::optional[:]","datePublished":"2019-04-15T18:29:53+00:00","dateModified":"2019-04-18T20:22:52+00:00","mainEntityOfPage":{"@id":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/"},"wordCount":2419,"commentCount":4,"publisher":{"@id":"https:\/\/yazilimperver.net\/#\/schema\/person\/534a27860d56170658018ac4c6f1af16"},"image":{"@id":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/#primaryimage"},"thumbnailUrl":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png","keywords":["boost::optional","c++ 17","has_value","in_place","Modern C++","nullable types","nullopt","std::any","std::move","std::optional","std::variant","value_or","weekly c++"],"articleSection":["C++","Modern C++"],"inLanguage":"tr","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/","url":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/","name":"[:tr]Haftal\u0131k C++ 14 \u2013 std::optional[:en]Weekly C++ 14 \u2013 std::optional[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131","isPartOf":{"@id":"https:\/\/yazilimperver.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/#primaryimage"},"image":{"@id":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/#primaryimage"},"thumbnailUrl":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png","datePublished":"2019-04-15T18:29:53+00:00","dateModified":"2019-04-18T20:22:52+00:00","breadcrumb":{"@id":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/#breadcrumb"},"inLanguage":"tr","potentialAction":[{"@type":"ReadAction","target":["https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/"]}]},{"@type":"ImageObject","inLanguage":"tr","@id":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/#primaryimage","url":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png","contentUrl":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png","width":32,"height":32},{"@type":"BreadcrumbList","@id":"https:\/\/yazilimperver.net\/index.php\/2019\/04\/15\/haftalik-c-14-stdoptional\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Ana sayfa","item":"https:\/\/yazilimperver.net\/"},{"@type":"ListItem","position":2,"name":"Haftal\u0131k C++ 14 \u2013 std::optional"}]},{"@type":"WebSite","@id":"https:\/\/yazilimperver.net\/#website","url":"https:\/\/yazilimperver.net\/","name":"Yaz\u0131l\u0131mperver'in D\u00fcnyas\u0131","description":"Payla\u015fmak g\u00fczeldir.","publisher":{"@id":"https:\/\/yazilimperver.net\/#\/schema\/person\/534a27860d56170658018ac4c6f1af16"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/yazilimperver.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"tr"},{"@type":["Person","Organization"],"@id":"https:\/\/yazilimperver.net\/#\/schema\/person\/534a27860d56170658018ac4c6f1af16","name":"yaz\u0131l\u0131mperver","image":{"@type":"ImageObject","inLanguage":"tr","@id":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2018\/04\/cropped-OnlyIcon-1.png","url":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2018\/04\/cropped-OnlyIcon-1.png","contentUrl":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2018\/04\/cropped-OnlyIcon-1.png","width":512,"height":512,"caption":"yaz\u0131l\u0131mperver"},"logo":{"@id":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2018\/04\/cropped-OnlyIcon-1.png"}}]}},"_links":{"self":[{"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/posts\/1107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/comments?post=1107"}],"version-history":[{"count":0,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/posts\/1107\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/media\/174"}],"wp:attachment":[{"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/media?parent=1107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/categories?post=1107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/tags?post=1107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}