type-deduction

Is auto in template parameter list in lambdas part of the standard?

浪子不回头ぞ 提交于 2020-02-27 23:01:11
问题 Today, I stumbled across the following code snippet: #include <utility> int main() { auto a = [](std::pair<auto, auto> value) { }; a(std::pair<int, bool>{ 3, true }); } http://cpp.sh/5p34 I have only one question: is this code supported by the standard? It compiles in GCC (with -std=c++14 ), but not clang or Visual Studio 2015 (VC++14). This seems like it should be part of the standard because if lambdas should have the same template support as regular functions, then this should be supported

Abbreviated function template vs. function template with forwarding reference param

家住魔仙堡 提交于 2020-01-12 07:30:33
问题 What are the differences between function templates with forwarding reference parameters template<typename T> void Universal_func(T && a) { } and abbreviated function templates ? void auto_fun(auto && a) { } Can I replace Universal_func with auto_fun ? Is Universal_func a of auto_fun or are they equal? I have tested the below program. It seems that both are the same. template<typename T> void Universal_func(T && a) { } void auto_fun(auto && a) { } int main() { int i; const int const_i = 0;

Why does iterator type deduction fail? [duplicate]

徘徊边缘 提交于 2020-01-09 11:56:40
问题 This question already has answers here : Why can't the template argument be deduced when it is used as template parameter to another template? (4 answers) Closed 6 years ago . Why does this not work in C++? Why can't I restrict foo 's parameter to std::vector<T>::iterator like this, and what is the best workaround? #include <vector> template<class T> void foo(typename std::vector<T>::iterator) { } int main() { std::vector<int> v; foo(v.end()); } The error is: In function ‘int main()’: error:

Why does iterator type deduction fail? [duplicate]

浪尽此生 提交于 2020-01-09 11:56:26
问题 This question already has answers here : Why can't the template argument be deduced when it is used as template parameter to another template? (4 answers) Closed 6 years ago . Why does this not work in C++? Why can't I restrict foo 's parameter to std::vector<T>::iterator like this, and what is the best workaround? #include <vector> template<class T> void foo(typename std::vector<T>::iterator) { } int main() { std::vector<int> v; foo(v.end()); } The error is: In function ‘int main()’: error:

C++11/14 and return( … ) vs return

给你一囗甜甜゛ 提交于 2020-01-04 03:47:29
问题 In C++ you are allowed to write a return statement that looks like : return ( ... ); which is different from the more popular : return ... ; In particular the first version returns the address/reference of something that is local to the stack of the function which contains that return statement. Now why something would like to return a reference to something that, at that point, has no lifetime ? What are the use case for this idiom ? Considering the new buzzword and features from C++11 and C

C++11/14 and return( … ) vs return

社会主义新天地 提交于 2020-01-04 03:47:02
问题 In C++ you are allowed to write a return statement that looks like : return ( ... ); which is different from the more popular : return ... ; In particular the first version returns the address/reference of something that is local to the stack of the function which contains that return statement. Now why something would like to return a reference to something that, at that point, has no lifetime ? What are the use case for this idiom ? Considering the new buzzword and features from C++11 and C

Why is direct-list-initialization with auto considered bad or not preferred?

我与影子孤独终老i 提交于 2020-01-01 08:50:09
问题 I've come into the habit of writing code with direct-list-initialization like below as it's more effective and it's very useful to prevent implicit narrowing: int i {0}; string s {""}; char c {'a'}; bool b {false}; auto num {100}; // But this?? But when it comes to the auto specifier, I have heard it is considered bad or not preferred to write it like that, why is that? 回答1: Here's an example of where using that syntax fails: struct Foo{}; void eatFoo (const Foo& f){} int main() { Foo a; auto

Function return type deduction in C++03

心已入冬 提交于 2020-01-01 07:01:11
问题 The tags ask the question, but nonetheless, consider the following: template<typename F, typename A, typename R> R call(F function, A arg) { return function(arg); } int foo(char) { return 5; } int main() { call(foo, 'a'); } The compiler happily compiles this if parameter R is removed and int is inserted manually as the return type. As shown, the compiler has no way of knowing what to make of R. How can I deduce function return types in C++03? I'm looking for methods that do not require

What does `new auto` do?

六眼飞鱼酱① 提交于 2019-12-30 03:44:24
问题 What does it mean when I use new auto ? Consider the expression: new auto(5) What is the type of the dynamically allocated object? What is the type of the pointer it returns? 回答1: In this context, auto(5) resolves to int(5) . You are allocating a new int from the heap, initialized to 5 . (So, it's returning an int * ) Quoting Andy Prowl's resourceful answer, with permission: Per Paragraph 5.3.4/2 of the C++11 Standard: If the auto type-specifier appears in the type-specifier-seq of a new-type

What does `new auto` do?

不羁岁月 提交于 2019-12-30 03:43:47
问题 What does it mean when I use new auto ? Consider the expression: new auto(5) What is the type of the dynamically allocated object? What is the type of the pointer it returns? 回答1: In this context, auto(5) resolves to int(5) . You are allocating a new int from the heap, initialized to 5 . (So, it's returning an int * ) Quoting Andy Prowl's resourceful answer, with permission: Per Paragraph 5.3.4/2 of the C++11 Standard: If the auto type-specifier appears in the type-specifier-seq of a new-type