#include<iostream>structX{// 普通的 lvalue 版本 voidf()&{std::cout<<"Called f() on an lvalue."<<std::endl;}// 普通的 rvalue 版本 voidf()&&{std::cout<<"Called f() on an rvalue."<<std::endl;}// const lvalue 版本 voidf()const&{std::cout<<"Called f() on a const lvalue."<<std::endl;}// const rvalue 版本 voidf()const&&{std::cout<<"Called f() on a const rvalue."<<std::endl;}};intmain(){Xx;// x 是一个 lvalue constXcx;// cx 是一个 const lvalue // 调用 lvalue 和 const lvalue 版本 x.f();// 输出: Called f() on an lvalue. cx.f();// 输出: Called f() on a const lvalue. // 调用 rvalue 和 const rvalue 版本 X().f();// 输出: Called f() on an rvalue. constX().f();// 输出: Called f() on a const rvalue. return0;}
#include<iostream>structX{// lvalue 版本 voidf(thisX&){std::cout<<"Called f() on an lvalue (explicit). "<<std::endl;}// rvalue 版本 voidf(thisX&&){std::cout<<"Called f() on an rvalue (explicit). "<<std::endl;}// const lvalue 版本 voidf(thisconstX&)const{std::cout<<"Called f() on a const lvalue (explicit). "<<std::endl;}// const rvalue 版本 voidf(thisconstX&&)const{std::cout<<"Called f() on a const rvalue (explicit). "<<std::endl;}};intmain(){Xx;// x 是一个 lvalue constXcx;// cx 是一个 const lvalue // 调用 lvalue 和 const lvalue 版本 x.f();// 输出: Called f() on an lvalue (explicit). cx.f();// 输出: Called f() on a const lvalue (explicit). // 调用 rvalue 和 const rvalue 版本 X().f();// 输出: Called f() on an rvalue (explicit). constX().f();// 输出: Called f() on a const rvalue (explicit). return0;}
#include<iostream>template<typename...Ts>structoverload:Ts...{usingTs::operator()...;};intmain(){overloadf={[](inti){std::cout<<"int thingy\n";},[](floatf){std::cout<<"float thingy\n";}};f(2);//prints int thingyf(2.0f);//prints float thingyreturn0;}
output
PowerShell
12
intthingyfloatthingy
variant version
C++
1 2 3 4 5 6 7 8 91011121314151617181920
#include<iostream>#include<variant>template<typename...Ts>structoverload:Ts...{usingTs::operator()...;};intmain(){overloadf={[](inti){std::cout<<"int thingy\n";},[](floatf){std::cout<<"float thingy\n";}};std::variant<int,float>v=2.0f;std::variant<int,float>w=2;std::visit(f,v);//prints float thingystd::visit(f,w);//prints int thingyreturn0;}
output
PowerShell
12
floatthingyintthingy
Recursive Lambda (wrong)
C++
1 2 3 4 5 6 7 8 9101112
#include<iostream>intmain(){autof=[](inti){if(i==0)return1;returni*f(i-1);//error: 'f' declared with auto// cannot appear in its own initialiser!};std::cout<<f(5);return0;}
#include<iostream>#include<map>// might be optimized to compile-time if compiled decides...constintimportantNum=42;// will be inited at runtimestd::map<std::string,double>buildMap(){returnstd::map<std::string,double>();}intmain(){conststd::map<std::string,double>countryToPopulation=buildMap();return0;}
const example 2
C++
1 2 3 4 5 6 7 8 9101112
#include<iostream>#include<array>intmain(){constintcount=3;std::array<double,count>doubles{1.1,2.2,3.3};// but not double:constdoubledCount=3.3;std::array<double,static_cast<int>(dCount)>moreDoubles{1.1,2.2,3.3};return0;}
constevalintsum(inta,intb){returna+b;}constexprintsum_c(inta,intb){returna+b;}intmain(){constexprautoc=sum(100,100);static_assert(c==200);constexprautoval=10;static_assert(sum(val,val)==2*val);inta=10;intb=sum_c(a,10);// fine with constexpr function// int d = sum(a, 10); // error! the value of 'a' is // not usable in a constant expression}
example 2
C++
1 2 3 4 5 6 7 8 9101112131415161718192021
constevalintsum(inta,intb){returna+b;}constexprintsum_c(inta,intb){returna+b;}intmain(){constexprautoc=sum(100,100);static_assert(c==200);constexprautoval=10;static_assert(sum(val,val)==2*val);inta=10;intb=sum_c(a,10);// fine with constexpr functionintd=sum(a,10);// error! the value of 'a' is // not usable in a constant expression}
#include<array>// init at compile timeconstexprintcompute(intv){returnv*v*v;}constinitintglobal=compute(10);// won't work:// constinit int another = global;intmain(){// but allow to change later...global=100;// global is not constant!// std::array<int, global> arr;}