auto Person::GetPersonType() -> PersonType { return type_; }
有了decltype之後,城市就能這樣寫了
1 2 3 4 5 6 7 8
template <typename Builder> auto makeAndProcessObject (const Builder& builder) -> decltype( builder.makeObject() ) { auto val = builder.makeObject(); // do stuff with val return val; }
新的Return Value Syntax也應用在Lambda Expression上,有機會再說吧。
## Install yum utils ## $ yum install yum-utils ## Package-cleanup set count as how many old kernels you want left ## $ package-cleanup --oldkernels --count=2
template <typename B, typename T = int> void Bar(B b = B(), T t = T()) {} Bar(10); // Bar<int, int> Bar(10L); // Bar<long, int> Bar(10L, 20L); // Bar<long, long> Bar(); // Compile error, couldn't deduce template parameter ‘B’
同樣的在Function Template overload的時候,也是地雷區。
1 2 3 4 5
template <typename T = int> void Foo(T t = T()) {} template <typename B, typename T = int> void Foo(B b = B(), T t = T()) {} Foo(12L); // Compile error, ‘Foo(long int)’ is ambiguous // void Foo(T) [with T = long int] and void Foo(B, T) [with B = long int; T = int] Foo(); // Compile OK, choose the first function