return-type-deduction

Return type deduction for in-class friend functions

走远了吗. 提交于 2019-11-30 10:50:35
Here is a little experiment with return type deduction for in-class friend functions (using Clang 3.4 SVN and g++ 4.8.1 with std=c++1y in both cases) that is not documented in the linked working paper #include <iostream> struct A { int a_; friend auto operator==(A const& L, A const& R) { return L.a_ == R.a_; // a_ is of type int, so should return bool } }; template<class T> struct B { int b_; friend auto operator==(B const& L, B const& R) { return L.b_ == R.b_; // b_ is of type int, so should return bool } }; using BI = B<int>; int main() { std::cout << (A{1} == A{2}) << "\n"; // OK for Clang,

Is void{} legal or not?

痴心易碎 提交于 2019-11-30 08:09:35
This is a follow-up up of this question. In the comments and in the answer it is said more than once that void{} is neither a valid type-id nor a valid expression . That was fine, it made sense and that was all. Then I came through [7.1.7.4.1/2] ( placeholder type deduction ) of the working draft. There it is said that: [...] - for a non-discarded return statement that occurs in a function declared with a return type that contains a placeholder type, T is the declared return type and e is the operand of the return statement. If the return statement has no operand, then e is void{} ; [...] So,

Variadic template recursive return type deduction compilation error

▼魔方 西西 提交于 2019-11-30 04:23:20
问题 Why the code below does not compile? template <typename T> T sum(T t){ return t; } template <typename T, typename ...U> auto sum(T t, U... u) -> decltype(t + sum(u...)) { return t + sum(u...); } int main() { sum(1, 1.5, 2); } Compilation error: error: no matching function for call to ‘sum(int, double, int)’ sum(1, 1.5, 2); What would be a good workaround to implement this feature? 回答1: Here we forward the work to helper types: namespace details { template<class...Ts> struct sum_t {}; template

Is void{} legal or not?

梦想与她 提交于 2019-11-29 10:47:41
问题 This is a follow-up up of this question. In the comments and in the answer it is said more than once that void{} is neither a valid type-id nor a valid expression . That was fine, it made sense and that was all. Then I came through [7.1.7.4.1/2] ( placeholder type deduction ) of the working draft. There it is said that: [...] - for a non-discarded return statement that occurs in a function declared with a return type that contains a placeholder type, T is the declared return type and e is the

What is the return type of a lambda expression if an item of a vector is returned?

吃可爱长大的小学妹 提交于 2019-11-28 10:42:31
Consider the following snippet: #include <iostream> #include <vector> #include <functional> int main() { std::vector<int>v = {0,1,2,3,4,5,6}; std::function<const int&(int)> f = [&v](int i) { return v[i];}; std::function<const int&(int)> g = [&v](int i) -> const int& { return v[i];}; std::cout << f(3) << ' ' << g(3) << std::endl; return 0; } I was expecting the same result: in f , v is passed by const reference, so v[i] should have const int& type. However, I get the result 0 3 If I do not use std::function, everything is fine: #include <iostream> #include <vector> #include <functional> int

construction helper make_XYZ allowing RVO and type deduction even if XZY has noncopy constraint

╄→尐↘猪︶ㄣ 提交于 2019-11-28 07:38:59
UPDATE1: C++17 added type deduction for constructors - which does not imply that the free function is an inferior solution. UPDATE2: C++17 added guaranteed copy elision (the copy does not even take place conceptually). So with C++17 my code actually works and with optimal performance. But Martinho's code using brace initialisation for the return value is still the cleaner solution I believe. But checkout this answer from Barry and the comment from T.C. OLD POST: Type deduction does not work for constructors (at least until and including C++11). The common solution is to rely on RVO (Return

What is the return type of a lambda expression if an item of a vector is returned?

最后都变了- 提交于 2019-11-27 03:45:05
问题 Consider the following snippet: #include <iostream> #include <vector> #include <functional> int main() { std::vector<int>v = {0,1,2,3,4,5,6}; std::function<const int&(int)> f = [&v](int i) { return v[i];}; std::function<const int&(int)> g = [&v](int i) -> const int& { return v[i];}; std::cout << f(3) << ' ' << g(3) << std::endl; return 0; } I was expecting the same result: in f , v is passed by const reference, so v[i] should have const int& type. However, I get the result 0 3 If I do not use

When should I use C++14 automatic return type deduction?

[亡魂溺海] 提交于 2019-11-26 23:32:38
With GCC 4.8.0 released, we have a compiler that supports automatic return type deduction, part of C++14. With -std=c++1y , I can do this: auto foo() { //deduced to be int return 5; } My question is: When should I use this feature? When is it necessary and when does it make code cleaner? Scenario 1 The first scenario I can think of is whenever possible. Every function that can be written this way should be. The problem with this is that it might not always make the code more readable. Scenario 2 The next scenario is to avoid more complex return types. As a very light example: template<typename

What are some uses of decltype(auto)?

对着背影说爱祢 提交于 2019-11-26 14:03:14
In c++14 the decltype(auto) idiom is introduced. Typically its use is to allow auto declarations to use the decltype rules on the given expression . Searching for examples of "good" usage of the idiom I can only think of things like the following (by Scott Meyers ), namely for a function's return type deduction : template<typename ContainerType, typename IndexType> // C++14 decltype(auto) grab(ContainerType&& container, IndexType&& index) { authenticateUser(); return std::forward<ContainerType>(container)[std::forward<IndexType>(index)]; } Are there any other examples where this new language

When should I use C++14 automatic return type deduction?

不羁岁月 提交于 2019-11-26 08:46:03
问题 With GCC 4.8.0 released, we have a compiler that supports automatic return type deduction, part of C++14. With -std=c++1y , I can do this: auto foo() { //deduced to be int return 5; } My question is: When should I use this feature? When is it necessary and when does it make code cleaner? Scenario 1 The first scenario I can think of is whenever possible. Every function that can be written this way should be. The problem with this is that it might not always make the code more readable.