ããã§ããHabrãå®æçã«èŠãŠãæ°ããæ¬ã«é¢ããåºçç©ãèŠã€ããããŸããã§ãããèªåã§æžãå¿ èŠãããããã§ãã ãã¡ãããç§ã¯å®å šãªç¿»èš³ã«ã¯ååã§ã¯ãªãã®ã§ãç°¡åã«çµããæ§ããã«æ³šéãšåŒã¶ããšã«ããŸããã ç§ã¯ãŸããçŽ æãåç·šæããèªç±ãåããŸããããã®é åºã®çãèšãçŽããããè¯ãããã«æããŸãã ãã¹ãŠã®ã³ãŒãäŸã¯æ¬ããçŽæ¥åŒçšãããŠãããæã è¿œå ãããŠããŸãã
1ã€ã®èŠåïŒMyers ã¯æ§æã説æããŠããŸãããèªè ãããŒã¯ãŒããã©ã ãåŒã®æžãæ¹ãªã©ãç¥ã£ãŠãããšæ³å®ãããŠããŸã ã ãããã£ãŠã誰ãããã®æ¬ããC ++ 11/14ã®åŠç¿ãéå§ããããšã«æ±ºããå Žåãåç §çšã«è¿œå è³æã䜿çšããå¿ èŠããããŸãã ãã ããããã¯åé¡ã§ã¯ãªããã¯ã³ã¯ãªãã¯ã§ãã¹ãŠãã°ãŒã°ã«ã«ãªããŸãã
C ++ 98ããC ++ 11/14ãžã ãã¹ãŠã®ãã¥ãŒã¹ã®ã®ã£ããã
èªå -äžèŠãããšãããã¯åãªã倧ããªç ç³ã®ã¹ããŒã³ã«ãããŸããããæ¬è³ªã§ã¯ãªãã«ããŠããC ++ã³ãŒãã®åœ¢åŒãå€ããããšãã§ããŸãã Straustrupã¯ã1983幎ã«çŸåšã®æå³ã§ãã®ããŒã¯ãŒãïŒå ·äœçã ãCã§ã¯ç¡çšïŒãå°å ¥ããããšããŠããããCã³ãã¥ããã£ããã®å§åã®äžã§ãã®èããæŸæ£ããããšãå€æããã ããã«ããã³ãŒããã©ã®ããã«å€æŽããããã確èªããŠãã ããã
template<typename It> void dwim(It b, It e) { while(b != e) { typename std::iterator_traits<It>::value_type value=*b; .... } } template<typename It> void dwim(It b, It e) { while(b != e) { auto value=*b; ... } }
2çªç®ã®äŸã¯åã«çãã ãã§ãªããããã§ã¯ãå®å šã«äžèŠãªæ£ç¢ºãªåŒ* bãé衚瀺ã«ããŸãã ããã«ãæ¬è³ªçã«ãåŒstd :: iterator_traits <It> :: value_typeã¯ãåç §è§£é€æã«ååŸãããã€ãã¬ãŒã¿ãŒã®ã¿ã€ãã決å®ããããã«STLã®dææã«çºæãããç¬åµçãªæŸèæã«ãããŸããããã ãã2çªç®ã®å¯äžã®æŒç®åã«ã¯*ïŒïŒãå¿ èŠã§ãã æŸèæã§ããŠã³ïŒ
çŽåŸã§ããªãïŒ ããã«å¥ã®äŸããããŸãããç§ã®æèŠã§ã¯åãªããã©ãŒã§ãïŒ
std::unorderd_map<std::string,int> m; for(std::pair<std::string,int>& p : m) { ... }
ãã®ã³ãŒãã¯ã³ã³ãã€ã«ãããŸããã
蚌æ
ãäºå®ã¯ãstd :: unordered_map <std :: stringãint>ã®æ£ããåã¯std :: pair < const std :: stringãint>ã§ãããšããããšã§ããããŒã¯å®æ°ã§ãªããã°ãªããªãã®ã¯æããã§ããã autoã䜿çšããæ¹ãæ£ç¢ºã§ãããããã¯ããã«ç°¡åã§ãé ã®äžã®è¡šçŸã®ã¿ã€ãã
auto1.cc:8:38ïŒãšã©ãŒïŒã¿ã€ãstd ::ãã¢<std :: basic_string <char>ãint>ã®åç
§ã®ç¡å¹ãªåæåïŒã¿ã€ãstd ::ãã¢<const std :: basic_string <char>ã®åŒããã int>
èšèªã«å³å¯ããè¿œå ããããã€ãã®ãã€ã³ãïŒ
int x1=1; //1 int x2; //2 ! auto x3=1; //3 auto x4; //4 ! std::vector<int> v; unsigned x5=v.size(); //5 size_t, auto x6=v.size(); //6 int f(); int x7=f(); //7 f() ? auto x8=f(); //8
ãããã®äŸãããããããã«ã autoãäœç³»çã«äœ¿çšãããšããããã°æã«å€ãã®ç¥çµãç¯çŽã§ããŸãã
ãããŠæåŸã«ã autoããªããã°ãã©ã ãåŒã¯åã«äžå¯èœã§ãïŒ
auto derefUPLess= [](const std::unique_ptr<Widget>& p1, const std::unique_ptr<Widget>& p2) { return *p1 < *p2; };
ãã®å Žåãæ£ç¢ºãªåderefUPLessã¯ã³ã³ãã€ã©ãŒã®ã¿ãç¥ã£ãŠããã autoã䜿çšããã«å€æ°ã«æ ŒçŽããããšã¯ã§ããŸããã ãã¡ããã次ã®ããã«æžãããšãã§ããŸãã
std::function<bool (const std::unique_ptr<Widget>&, const std::unique_ptr<Widget>&)> derefUPLess= [](const std::unique_ptr<Widget>& p1, const std::unique_ptr<Widget>& p2) { return *p1 < *p2; };
ãã ããstd :: function <>ãšlambdaã¯åãåã§ã¯ãªããããã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãããããããããŒãã«ã¡ã¢ãªãå²ãåœãŠãããŸããããã«ãstd :: function <>ã®åŒã³åºãã¯ãã©ã ãé¢æ°ãçŽæ¥åŒã³åºããããé«äŸ¡ã«ãªãããšãä¿èšŒãããŠããŸã ã
ãããŠæåŸã«-è»èã®ããšã èªåã¯äžæ¬åŒ§ã§åæåããããšãã«ç°ãªãåäœãããŸãïŒ
int x1=1; int x2(1); int x3{1}; int x4={1};
ãã ãããããã®åŒã¯ãã¹ãŠå®å šã«åçã§ãã
auto x1=1; auto x2(1); auto x3{1}; auto x4={1};
x1ãšx2ã¯intåã§ãããx3ãšx4ã¯ç°ãªãåstd :: initializer_list <int>ã«ãªããŸãã autoã{}ã€ãã·ã£ã©ã€ã¶ãŒã«åºäŒããšããã«ããã®ãããªæ§æäœã®å éšC ++å-std :: initializer_list <>ãè¿ããŸãã ãªããããªã®ãããã€ã€ãŒãºã§ãã圌ãç¥ããªãããšãèªããŠãããç§ãæšæž¬ããŸããã
decltype-ããã§ã¯ãã¹ãŠãå€ããå°ãªããåçŽã§ãããã®èšèšã¯ããã³ãã¬ãŒããç¹ã«ãã³ãã¬ãŒããã©ã¡ãŒã¿ã«å¿ããæ»ãå€ã®åãæã€é¢æ°ã®èšè¿°ããã䟿å©ã«ããããã«è¿œå ãããŸããã
template<typename Container, typename Index> auto access(Container& c, Index i) -> decltype(c[i]) { .... return c[i]; }
ããã§ã autoã¯é¢æ°åã®åŸã«æ»ãå€ã®åã瀺ãããããšãåã«ç€ºãã decltypeïŒïŒã¯æ»ãå€ã®åãéåžžã¯ã³ã³ããã®içªç®ã®èŠçŽ ãžã®åç §ã決å®ããŸãããäžè¬çãªå Žåãæ£ç¢ºã«c [i]ãè¿ããã®ã¯äœã§ãã
çµ±äžãããåæå -ååãæ°ããæšæºã§æ瀺ããŠããããã«ãå€æ°ãåæåããæ®éçãªæ¹æ³ãå°å ¥ããããšããŸããããããã¯åé¡ãããŸãããããšãã°ã次ã®ããã«èšè¿°ã§ããŸãã
std::vector<int> v{1,2,3}; // sockaddr_in sa={AF_INET, htons(80), inet_addr("127.0.0.1")};
ããã«ãäžæ¬åŒ§ã䜿çšããŠãã¯ã©ã¹ã®ééçã¡ã³ããŒãåæåããããšãã§ããŸãïŒéåžžã®æ¬åŒ§ã¯æ©èœããŸããïŒã
class Widget { ... int x{0}; int y{0}; int z{0}; };
ãŸããã¯ããŒãŒããã®äžã§åžžã«è¶³å ã«ãã£ãåžžç·ã®ã¬ãŒããæçµçã«é ããŸããç¹ã«ãã³ãã¬ãŒãéçºè ãæ©ãŸãã®ã¯æ¬¡ã®ãšããã§ãã
Widget w1(); // , // Widget w2{}; //
ãŸããèšèªã®å³å¯æ§ã«åãããã1ã€ã®ã¹ãããã§ãããæ°ããåæåã«ããã粟床ã®äœäžã䌎ãåå€æïŒçž®å°å€æïŒãé²æ¢ãããŸãã
double a=1, b=2; int x=a+b; // fine int y={a+b}; // error
ããã...ããã¹ãŠåãããã«ãäœããããŸããããªãã£ããšããæèŠã¯ãŸã æ®ã£ãŠããŸããã ãŸããäžæ¬åŒ§ãå«ãŸããå Žåãåæåã¯åžžã«å éšã¿ã€ãstd :: initializer_list <>ãä»ããŠè¡ãããŸãããäœããã®çç±ã§ãã¯ã©ã¹ããã®ãã©ã¡ãŒã¿ãŒãæã€ã³ã³ã¹ãã©ã¯ã¿ãŒã®1ã€ãå®çŸ©ããå Žåããã®ã³ã³ã¹ãã©ã¯ã¿ãŒã¯åžžã«ã³ã³ãã€ã©ãŒã«ãã£ãŠåªå ãããŸãã äŸïŒ
class Widget { Widget(int, int); Widget(std::initializer_list<double>); }; Widget w1(0, 0); // calls ctor #1 Widget w2{0, 0}; // calls ctor #2 !?
ãã¹ãŠã®èšŒæ ã«åããŠã2çªç®ã®ã±ãŒã¹ã§ã¯ãã³ã³ãã€ã©ãŒã¯çæ³çãªé©åãªconstructor_1ãç¡èŠããconstructor_2ãåŒã³åºããŠãintãdoubleã«å€æããŸãã ã¡ãªã¿ã«ãã¯ã©ã¹å®çŸ©ã§intåãšdoubleåã亀æãããšã{doubleãdouble}ã®std :: initializer_list <int>ãžã®å€æã粟床ã倱ãããã«ãã³ãŒãã®ã³ã³ãã€ã«ãéåžžåæ¢ããŸãã
ãã®è¡çªã¯ãC ++ 11ã®èŠåã«åŸã£ãŠãçŸåšã©ã®ã³ãŒãã§ãçºçããå¯èœæ§ããããŸãã
std :: vectorïŒ10ã20ïŒã¯10èŠçŽ ã®ãªããžã§ã¯ããäœæããŸããã
std :: vector {10ã20}ã¯ã2ã€ã®èŠçŽ ã®ã¿ãããªããžã§ã¯ããäœæããŸãã
äžèšããããã¹ãŠããã£ã«ãã©ã³ãã§è£ 食ããŸã-ã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒãšã ãŒãã³ã³ã¹ãã©ã¯ã¿ãŒã§ã¯ããã®ã«ãŒã«ã¯æ©èœããŸããïŒ
class Widget { Widget(); Widget(const Widget&); Widget(Widget&&); Widget(std::initializer_list<int>); operator int() const; }; Widget w1{}; Widget w2{w1}; Widget w3{std::move(w1)};
æåéãæ³ã®æåã«åŸããã³ã³ãã€ã©ãstd :: initializer_listãã©ã¡ãŒã¿ã䜿çšããŠã³ã³ã¹ãã©ã¯ã¿ãéžæããå®éã®ãã©ã¡ãŒã¿ãintïŒïŒæŒç®åãä»ããŠå€æãããããšãæåŸ ããŸãã ãã®å ŽåïŒã³ããŒ/移åã³ã³ã¹ãã©ã¯ã¿ãŒïŒãåŒã³åºãããã®ã¯ã³ããŒã³ã³ã¹ãã©ã¯ã¿ãŒã§ãã
äžè¬ã«ãäžžæ¬åŒ§ãŸãã¯äžæ¬åŒ§ã®ããããã®ã¿ã€ãã®æ¬åŒ§ãåžžã«äœ¿çšãããšããæšå¥šäºé ã¯ãééããªãæ©èœããŸããã ãã€ã€ãŒãºã¯ã1ã€ã®æ¹æ³ã«åºå·ããå¿ èŠãªå Žåã«ã®ã¿ä»ã®æ¹æ³ãé©çšããããšããå§ãããŸãã圌èªèº«ã¯æ¬åŒ§ã«åŸããŠããŸãã ãã ãããã³ãã¬ãŒãã«ã¯åé¡ãæ®ã£ãŠãããåå ã¯ãã³ãã¬ãŒããã©ã¡ãŒã¿ã«ãã£ãŠæ±ºå®ãããŸããå°ãªããšããC ++ã¯éå±ãªèšèªã®ãŸãŸã§ãã
nullptr-話ãããšãããããŸãããæããã«NULLãš0 ã¯ãã€ã³ã¿ãŒã§ã¯ãããŸãã ãããã¯ããªãŒããŒããŒããããé¢æ°ãåŒã³åºããŠãã³ãã¬ãŒããå®è£ ãããšãã«å€ãã®ãšã©ãŒã«ã€ãªãããŸãã ãã®å Žåã nullptrã¯ãã€ã³ã¿ãŒã§ããããšã©ãŒã«ã¯ãªããŸããã
ãšã€ãªã¢ã¹å®£èšãštypedef
éåžžã®å宣èšã®ä»£ããã«
typedef std::unique_ptr<std::unordered_map<std::string,std::string>> UPtrMapSS;
ãã®èšèšã䜿çšããããšãææ¡ããŸã
using UPtrMapSS=std::unique_ptr<std::unordered_map<std::string,std::string>>;
ããã2ã€ã®åŒã¯å®å šã«åçã§ããã話ã¯ããã§çµããã§ã¯ãããŸããããšã€ãªã¢ã¹ã¯ãšã€ãªã¢ã¹ãã³ãã¬ãŒããšããŠäœ¿çšã§ããããã«ããæè»æ§ãé«ãŸããŸãã
template<typename T> using MyAllocList=std::list<T, MyAlloc<T>>; MyAllocList<Widget> lw;
C ++ 98ã§ã¯ããã®ãããªæ§é ãäœæããããã«ãMyAllocListã¯ãã³ãã¬ãŒãæ§é ã宣èšãããã®äžã®åã宣èšãã次ã®ããã«äœ¿çšããå¿ èŠããããŸãã
MyAllocList<Widget>::type lw;
ããããç©èªã¯ç¶ããŸãã typedefãä»ããŠå®£èšãããåããã³ãã¬ãŒãã¯ã©ã¹å ã®äŸååãšããŠäœ¿çšããå Žåãè¿œå ã®ããŒã¯ãŒãã䜿çšããå¿ èŠããããŸã
template<typename T> class Widget { typename MyAllocList<T>::type lw; ...
æ°ããæ§æã§ã¯ãã¹ãŠãã¯ããã«åçŽã§ã
template<typename T> class Widget { MyAllocList<T> lw; ...
äžè¬ã«ãã¡ã¿ããã°ã©ãã³ã°ã¯ããã®æ§ææ§é ã«ããã¯ããã«ç°¡åã«ãªããšçŽæãããŠããŸãã ããã«ãC ++ 14以éã§ã¯ã察å¿ããå矩èªã<type_traits>ã«å°å ¥ãããŸããã€ãŸããéåžžã®ä»£ããã«
typename remove_const<...>::type // remove_const_t<...>
ã·ããã ã䜿çšããããšã¯éåžžã«æçšãªç¿æ £ã§ãããä»ããèªåèªèº«ã§èäœãéå§ããå¿ èŠããããŸãã ãã€ãŠã typedefã¯ãã¯ãã容赊ãªãåŠçããŸãããå¿ãããèš±ãããåãã³ã€ã³ã§ãããè¿æžããŸãã
scoped enumsã¯ãèšèªã®å éšèª¿åã«åãããã1ã€ã®ã¹ãããã§ãã å®éãã¯ã©ã·ãã¯åæåã¯ãããã¯å ã§å®£èšãããŸãããããããã®ã¹ã³ãŒãã¯ã°ããŒãã«ãªãŸãŸã§ããã
enum Color { black, white, red };
Colorãšåããããã¯ã«é»ãçœãèµ€ã衚瀺ãããŠãããããåå空éã®ç«¶åãšç®è©°ãŸããçºçããŸãã æ°ããæ§æïŒ
enum class Color { black, white, red }; Color c=Color::white;
ã¯ããã«ãšã¬ã¬ã³ãã«èŠããŸãã ã²ãšã€ã ã-åæã«ãåæåããæŽæ°åãžã®èªåå€æãåé€ãããŸãã
int x=Color::red; // int y=static_cast<int>(Color::white); // ok
ããã¯ç¢ºãã«èšèªã®å³å¯ããå¢ãã ãã§ãããç§ãèŠãã³ãŒãã®å€§éšåã§ã¯ãå°ãªããšãswitchãžã®è»¢éãŸãã¯std :: coutãžã®åºåã®ããã«ãenumãäœããã®åœ¢ã§intã«å€æãããŸãã
ãªãŒããŒã©ã€ããåé€ãããã³ããã©ã«ãã¯ãé¢æ°ã宣èšãããšãã«åœ¹ç«ã€æ°ããåèªã§ãã
ã¯ã©ã¹ã®ãã®ä»®æ³ã¡ã³ããŒé¢æ°ãåºæ¬ã¯ã©ã¹ã®ç¹å®ã®é¢æ°ããªãŒããŒã©ã€ãããå¿ èŠãããããšãã³ã³ãã€ã©ã«éç¥ããé©åãªãªãã·ã§ã³ããªãå Žåã¯ã芪åã«ãšã©ãŒãéç¥ããŸãã ãããããã©ã³ãã ãªã¿ã€ããã¹ã眲åã®å€æŽã«ãã£ãŠä»®æ³é¢æ°ãéåžžã®é¢æ°ã«å€ããç¶æ³ã«ééããå¯èœæ§ããããŸããæãäžæå¿«ãªããšã¯ããã¹ãŠãæ£åžžã«ã³ã³ãã€ã«ãããããšã§ãããã©ããããããåäœãç°ãªããŸãã ãããã£ãŠãããã¯äºåºŠãšèµ·ãããŸããã 䜿çšã匷ããå§ãããŸãã
delete-å€ãïŒãããŠçŸããïŒããªãã¯ãããã©ã«ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒãšä»£å ¥æŒç®åã®private宣èšã§çœ®ãæããããšãç®çãšããŠããŸãã èŠãç®ã¯äžè²«ããŠããŸãããããã ãã§ã¯ãããŸããã ãã®ææ³ã¯ãäžèŠãªåŒæ°ã®å€æãé²ãããã«ãç¡æã®é¢æ°ã«ãé©çšã§ããŸãã
bool isLucky(int); bool isLucky(char) =delete; bool isLucky(bool) =delete; bool isLucky(double) =delete; isLucky('a'); // error isLucky(true); // error isLucky(3.5); // error
åãææ³ããã³ãã¬ãŒãã«äœ¿çšã§ããŸã
template<typename T> void processPointer(T*); template<> void processPointer(void*) =delete; template<> void processPointer(char*) =delete;
æåŸã®2ã€ã®å®£èšã¯ãããã€ãã®ã¿ã€ãã®åŒæ°ã«å¯Ÿããé¢æ°ã®çæãçŠæ¢ããŠããŸãã
ããã©ã«ã -ãã®ä¿®é£Ÿåã¯ãã³ã³ãã€ã©ãŒã«èªåã¯ã©ã¹é¢æ°ãçæããããããå®éã«äœ¿çšããå¿ èŠããããŸãã C ++ 98ã§èªåçã«çæãããé¢æ°ã«ã¯ããã©ã¡ãŒã¿ãŒãªãã®ã³ã³ã¹ãã©ã¯ã¿ãŒãã³ã³ã¹ãã©ã¯ã¿ãŒãã³ããŒãããã¹ãã©ã¯ã¿ãŒãããã³ä»£å ¥æŒç®åãå«ãŸããŠããŸããããããã¯ãã¹ãŠãå¿ èŠã«å¿ããŠããç¥ãããã«ãŒã«ã«åŸã£ãŠäœæãããŸããã C ++ 11ã§ã¯ã移åã³ã³ã¹ãã©ã¯ã¿ãŒãšä»£å ¥æŒç®åãè¿œå ãããŸããããèªåé¢æ°ãäœæããèŠåèªäœãå€æŽãããã ãã§ã¯ãããŸããã ããžãã¯ã¯åçŽã§ããèªåãã¹ãã©ã¯ã¿ã¯ã¯ã©ã¹ã¡ã³ããŒãšåºæ¬ã¯ã©ã¹ã®ãã¹ãã©ã¯ã¿ãé çªã«åŒã³åºããã³ããŒ/移åã³ã³ã¹ãã©ã¯ã¿ã¯ãã®ã¡ã³ããŒã®å¯Ÿå¿ããã³ã³ã¹ãã©ã¯ã¿ãé çªã«åŒã³åºããŸãã ãã ãããããã®é¢æ°ã®ãããããæåã§å®çŸ©ããããšã«çªç¶æ±ºããå Žåããã®åççãªåäœã¯ç§ãã¡ã«åãããã³ã³ãã€ã©ã¯åæ©ãç解ããããšãæåŠããŸãããã®å Žåã移åããã³ã³ã¹ãã©ã¯ã¿ãšä»£å ¥æŒç®åã¯èªåçã«äœæãããŸããã ãã¡ããããã®ããžãã¯ã¯ã³ããŒãã¢ã«ãé©çšã§ããŸãããäžäœäºææ§ã®ããã«ãã®ãŸãŸã«ããããšã[ä»ã®ãšãã]決å®ãããŸããã ã€ãŸããC ++ 11ã§ã¯ã次ã®ããã«èšè¿°ããã®ãçã«ããªã£ãŠããŸãã
class Widget { public: Widget() =default; ~Widget() =default; Widget(const Widget&) =default; Widget(Widget&&) =default; Widget& operator=(const Widget&) =default; Widget& operator=(Widget&&) =default; ... };
åŸã§ãã¹ãã©ã¯ã¿ãå®çŸ©ããããšã«ããå Žåãäœãå€ãããŸãããããããªããšã移åããé¢æ°ãåçŽã«æ¶ããŠããŸããŸãã ã³ãŒãã®ã³ã³ãã€ã«ã¯ç¶è¡ãããŸããã察å¿ããã³ããŒãåŒã³åºãããŸãã
noexept-æåŸã«ãæšæºã¯ãC ++ 98ã«ååšããäŸå€ã®ä»æ§ãéå¹ççã§ãããšèªèãããã®äœ¿çšãæãŸãããªãïŒ éæšå¥š ïŒãšèªèãã代ããã«1ã€ã®å€§ããªèµ€ããã©ã°ãèšå®ããŸã-noexceptã¯ãé¢æ°ãäŸå€ãã¹ããŒããªãããšã宣èšããŸã äŸå€ããŸã ã¹ããŒãããå Žåãããã°ã©ã ã¯çµäºããããšãä¿èšŒãããŸããã throwïŒïŒãšã¯ç°ãªããã¹ã¿ãã¯ã§ãããå·»ãæ»ããããšã¯éããŸããã ãã©ã°èªäœã¯å¹çæ§ã®èæ ®ããé€å€ãããŠããŸããã¹ã¿ãã¯ãææ Œã®æºåãã§ããŠããå¿ èŠããªãã ãã§ãªããã³ã³ãã€ã©ãŒã«ãã£ãŠçæãããã³ãŒãã¯ç°ãªãå ŽåããããŸãã 以äžã«äŸã瀺ããŸãã
Widget w; std::vector<Widget> v; ... v.push_back(w);
æ°ããèŠçŽ ããã¯ãã«ã«è¿œå ãããšããé ããæ©ãããå éšãããã¡å šäœãã¡ã¢ãªå ã§ç§»åããå¿ èŠãããç¶æ³ãçºçããŸããC++ 98ã§ã¯ãèŠçŽ ã¯1ã€ãã€ã³ããŒãããŸãã æ°ããæšæºã§ã¯ããã¯ãã«ã®èŠçŽ ã移åããããšã¯è«ççã§ãããæ¡éãã«å¹ççã§ããã泚æã1ã€ãããŸã...ã³ããŒåŠçäžã«ããããã®èŠçŽ ãäŸå€ãã¹ããŒããå Žåãæ°ããèŠçŽ ã¯èªç¶ã«ã¯æ¿å ¥ãããŸãããããã¯ãã«èªäœã¯éåžžã®ç¶æ ã®ãŸãŸã«ãªããŸãã èŠçŽ ã移åããå Žåããããã®äžéšã¯æ¢ã«æ°ãããããã¡ã«ãããäžéšã¯ãŸã å€ããããã¡ã«ãããã¡ã¢ãªãåäœç¶æ ã«åŸ©å ããããšã¯ãã§ã«äžå¯èœã§ãã 解決çã¯ç°¡åã§ããWidgetã¯ã©ã¹ã§ç§»åä»£å ¥æŒç®åãnoexceptãšããŠå®£èšãããŠããå Žåããªããžã§ã¯ãã¯ç§»åãããŸãããããã§ãªãå Žåã¯ã³ããŒãããŸãã
ããã§ãæ°ããã·ãŒãºã³ã®é·æã«ãããã¬ãã¥ãŒãçµäºããŸãã
constexpr ã std :: cbeginïŒïŒãªã©ãããã€ãã®ç¹ãæå³çã«çç¥ããŸããã ãããã¯éåžžã«åçŽã§ãããäœã話ãããšã¯ãããŸããã ç§ãè°è«ãããã®ã¯ãå®æ°ã¡ã³ããŒé¢æ°ã¯ã¹ã¬ããã»ãŒãã§ãªããã°ãªããªããšããçè«ã§ãããå察ã«ãæ§æãžã®åçŽãªè¿œå ã®ç¯å²ãè¶
ããŠãããããã³ã¡ã³ãã§ããŸãããã§ãããã
ã¿ã€ãããã®æŽŸçãããã³ããã«é¢é£ãããã¹ãŠ
C ++ 98ã®åæšå®ã¯ããã³ãã¬ãŒãã®å®è£ ãæ°ããæšæºã ãŠãããŒãµã«ãªã³ã¯ ãããŒã¯ãŒãautoããã³decltypeã§ã®ã¿äœ¿çšãããŸãã ã ã»ãšãã©ã®å ŽåãåŒãåºãã¯çŽæçã§ããã競åãçºçããåäœã¡ã«ããºã ãç解ãããšéåžžã«åœ¹ç«ã¡ãŸãã ãã®æ¬äŒŒã³ãŒããååŸããŸãã
template<typename T> void f(ParamType param); f(expr);
ããã§ã®äž»ãªããšã¯ãTãšParamTypeãäžè¬ã«2ã€ã®ç°ãªãã¿ã€ãã§ãããšããããšã§ããããšãã°ãParamTypeã¯const TïŒã§ãã æ£ç¢ºãªåTã¯ãå®éã®åexprãšParamTypeãã¥ãŒã®äž¡æ¹ãããã³ãã¬ãŒãã®å®è£ äžã«æšå®ãããããã€ãã®ãªãã·ã§ã³ãå¯èœã§ãã
- æãåçŽãªå Žåã¯ãParamTypeããã€ã³ã¿ãŒã§ããªã³ã¯ã§ããªãå ŽåãåŒã¯å€ã«ãã£ãŠé¢æ°ã«æž¡ããããã¹ãŠã®åç
§ãconst修食åã¯exprããåé€ãããçŽç²ãªåã¯æ®ããŸã
template<typename T> void f(T param); int x=1; const int cx=x; const int& rx=x; f(x); // param - int f(cx); f(rx);
- ParamTypeããã€ã³ã¿ãŒãŸãã¯éåžžã®ïŒãŠãããŒãµã«ã§ã¯ãªãïŒãªã³ã¯ã§ããå Žåãã¿ã€ãTã掟çãããšããªã³ã¯ã¯åé€ãããŸãããconst / volatile修食åã¯ä¿åãããŸã
template<typename T> void f(T& param); int x=1; const int cx=x; const int& rx=x; f(x); // - int, param - int& f(cx); // - const int, param - const int& f(rx); // - const int, param - const int&
çŽæçã«ã¯ããã¹ãŠãå®å šã«ééçã§ããããã³ãã¬ãŒãã«ç€ºãããŠããããã«åç §ã«ãã£ãŠå€ãæž¡ããŸããã転éããããªããžã§ã¯ããžã®ã¢ã¯ã»ã¹æš©ã䟵害ããªãããã«èªã¿åã/æžã蟌ã¿ä¿®é£Ÿåãä¿åããŸãã
- ParamTypeãæ±çšåç
§ã§ããå ŽåãåŒã®ã¿ã€ãã¯exprã®ã¿ã€ãã«äŸåããŸãã 巊蟺å€ã®å ŽåãTãšParamTypeã®äž¡æ¹ããªã³ã¯ãšããŠæ±ãããexpr- rvalueã®å Žåãéåžžã®ãªã³ã¯ãšåæ§ã®ã«ãŒã«ãé©çšãããŸãã
template<typename T> void f(T&& param); int x=1; const int cx=x; const int& rx=x; // - lvalue f(x); // - int&, param - int& f(cx); // - const int&, param - const int& f(rx); // - const int&, param - const int& // f(1); // - int, param - int&&
autoã®å Žåãåæšè«ã®ã«ãŒã«ã¯ãŸã£ããåãã§ãããã®å Žåã autoã¯Tãã©ã¡ãŒã¿ãŒã®åœ¹å²ãæãããŸãããã ããåè¿°ã®1ã€ã®äŸå€ã¯ãããŸããautoãäžæ¬åŒ§ã§åŒãèŠããšã std :: initializer_liståã衚瀺ãããŸãã
decltypeã®å Žåã圌ã«æž¡ãããåã¯
int x=1; decltype(x); // x -, int decltype((x)); // (x) - , int&
ããããããã¯ããã¯ããç©æ¥µçã«äœ¿çšããŠããã©ã€ãã©ãªä»¥å€ã®äººãå·ã€ããããšã¯ã»ãšãã©ãããŸããã
ç§ã¯æžããããã®ãèªã¿çŽããŸãããäœããããããå€æããŸããã ããããæãèå³æ·±ãã®ã¯ããããã§ãããããã2ã€ã®æçš¿ã«åå²ããæ¹ãè¯ãã§ãããã ç¶ç¶ããã