Implicit type in lambda capture - c++

I'm starting out with C++ programming, and am curious why this is legal:
auto myFun = [n=0]() mutable {return n++;};
I would have thought this wouldn't work, as C++ is a strong typed language, but it seems that the compiler infers the integer type?

Lambdas, were introduced with C++11. There were no default initializer values in C++11:
auto myFun = [n]() { /* body */ };
That's all you had in C++11. Whatever the type of n was, that's what you captured, type and value.
Default initialization values were introduced with C++14. I suppose that it would've been possible to change the syntax so that the initialized captured variables used a complete, full-fledged declaration, something like:
auto myFun = [int n=0]() mutable {return n++;};
That might've been possible, but this wasn't really necessary. Even though C++14's default capture values do not explicitly state their types, their types are inferred from their initialization expressions just as strongly as if they were explicitly declared. And the resulting change in syntax is minimal. With:
auto myFun = [n=0]() mutable {return n++;};
the type of n is int, just as "strong" as if it were explicitly declared. It is not a char, and it is not a short. It is an int. End of story.
Also, keep in mind that with:
template<typename Arg> void function(Arg arg)
when this is invoked the type of Arg gets deduced, and it becomes a bone-fide, strong type, too. So this is really no different than template parameters: in the end when instantiated their types are still as strong as they are in the rest of C++.

From the reference on lambda captures:
A capture with an initializer acts as if it declares and explicitly captures a variable declared with type auto, ...
This means the [n = 0] is basically treated as if it's
auto n = 0;
The placeholder type is deduced by the compiler as int, and the same type inference happens in the lambda capture.
This convenient syntax of not needing to say auto in the initializer of a lambda capture is just that, a convenience. This syntax doesn't result in any changes to the type safety imposed by the language.

Related

decltype on the variable of reference type with curly braces

Consider the following code:
#include <type_traits>
int main() {
const int& p = 42;
auto v1 = decltype(p){};
static_assert(std::is_same_v<decltype(v1), int>);
decltype(p) v2{};
static_assert(std::is_same_v<decltype(v2), const int&>);
// auto v3 = X(const int&)X {};
}
Type of v1 is deduced as int. At the same time type of v2 is expectedly deduced as const int&. I think the first step for v1 could be treated as adding one more type alias using T = decltype(p); and then auto v4 = T{};. How this expression (decltype(p){} or T{}) is treated by compiler? I know that {} part is for instantiation, but how the resulting type of v1 is not a reference type?
Yet another question: is there a way to declare v3 variable of the same type as v1 using explicitly noted type const int& (instead of decltype(p))?
Any links to standard would be appreciated.
(To the downvoter(s): if you downvoted because you think quoting Scott Meyers is not equivalent to quoting the standard, oh well...)
As you can read from Effective Modern C++ (augmented with the part of the errata that you can reach by searching for Case 2: at that link, and that just makes the following read simpler, but it's not essential to the question):
If ParamType is a non-reference [...] if expr's type is a reference, ignore the reference part. If [...] expr is const, ingore that too. If it's volatile, also ignore that.
where param is the declaration specifier, which in your case is just auto, i.e. a non-reference.
In other words, you're creating v1 via plain auto (not auto&), i.e. by copy, so it does not matter whether you are initializing it with an entity which is reference or not, or even with const or not (or volatile or not, fwiw), because you're copying it.
Think about the simpler case,
int i = 3;
int& p = i;
auto v1 = p;
as far as v1 is concerned, it's really not important whether it is initalized with one (i) or the other (p) name by which the same entity is known, because it will get a copy of whatever value that entity has.
auto type deduction works just like template type deduction (except for a difference in how they deal with braced initializer, which is not relevant in this case), and for both of them you can refer to Scott Meyers' Effective Modern C++.
auto v4 = T{};. How this expression (decltype(p){} or T{}) is treated by compiler? I know that {} part is for instantiation, but how the result type is not a reference type?
The result decltype(p){} is a reference type. It's the usage of auto that drops the const and reference qualifiers. The deduction of the type is explained here, and they're the same as those used for template type deduction. You could use decltype(auto) instead to keep those qualifiers (or, in this particular case, you could use const auto&).

Why lambda expression's capture list cannot be decomposed using structured bindings

Before you throw a rotten tomato
I know the practical application of lambda decomposition is currently limited
as one wouldn't be able to find substitution-failure-friendly way to check
the number of lambda captures hidden in decomposed variables. This is just a theoretical question as I failed to find any standard part covering the capture member variable access modifiers.
Example
int main() {
int a;
auto [x] = [a]{};
static_cast<void>(a);
static_cast<void>(x);
return 0;
}
Standard reference
The standard section about lambda capture is quite long so I might have missed the relevant fragment. What I noticed is that there is an emphasis on that the non-static members which corresponds to the captures are/have to be unnamed.
I'd say this is unspecified by the Standard, but certainly intended to not work. What we know about lambda structure is that, from [expr.prim.lambda.closure]:
The type of a lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type
and
The closure type is not an aggregate type
and, from [expr.prim.lambda.capture]:
For each entity captured by copy, an unnamed non-static data member is declared in the closure type. The declaration order of these members is unspecified.
and:
It is unspecified whether additional unnamed non-static data members are declared in the closure type for entities captured by reference. If declared, such non-static data members shall be of literal type.
The intent of having unnamed members is to avoid having them being accessed outside of the lambda's body. The consequence of these members additionally being in unspecified order means as soon as you have more than one capture by copy, you wouldn't even be able to know what your structured binding did.
int a=1, b=2;
auto [x, y] = [a, b]{}; // x==1 or x==2??
The consequence of captures by reference not necessarily naming members means that you wouldn't even know how many identifiers to list in your structured binding declaration.
Since the access of the non-static data members is unspecified, it's possible to have a conforming implementation make them all public, which would satisfy case 3 of structured bindings. But that very much goes against the intent of both the way lambdas are structured and how structured bindings are supposed to work, so I'd be surprised if any implementation knowingly did this. gcc, for instance, explicitly patched to disallow it.
Why lambda expression's capture list cannot be decomposed using structured bindings
It actually can. The following
template<class... Ts> struct overload : Ts... { using Ts::operator()...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;
int main()
{
auto f = [x = 1, y = 2]() { return x + y; };
// auto [a, b] = f; // error: cannot decompose lambda closure type 'main()::<lambda()>'
overload o { f, };
auto [a, b] = o;
return b; // returns 2
}
works in GCC trunk https://godbolt.org/z/15c90z.

When should I use decltype(x) instead of auto to declare the type of a variable?

I see decltype(x) used inside macros where x is a variable name because the type of the object isn't known inside macros.
For example:
decltype(x) y = expr;
I could just have easily use auto instead of decltype. So what are those situations where decltype is needed for a variable type declaration instead of auto?
decltype becomes handy when you need to return some unknown type, which is evaluated during compilation:
template<class A, class B>
void MultiplyAB(A a, B b, decltype(a*b)& output)
{
output = a * b;
}
Additionally, if you don't like the way the output is handled by a reference, then you can also use the late-specified return type (and also use the decltype):
template<class A, class B>
auto MultiplyAB(A a, B b) -> decltype(a*b)
{
return a * b;
}
All of this, and more, is described by B. Stroustrup in the C++ FAQ.
You should use it when the required type of y is:
different (or potentially different) from the type of expr. If it was the same then auto would be more concise.
similarly for auto & or other modifications of the type of expr that auto can express.
and one of the following:
dependent on something in the surrounding code (i.e. not always the same type) and difficult to write using type traits or similar. This will tend to happen in template code. There might be a type trait that you can use to get the required type from the template parameters, but then again there might not so a use of decltype would save you defining one.
always the same type, (or dependent on template parameters in a way that is easy to express using existing type traits or similar) but the type is very long-winded to write and there is a much shorter and clear expression you can use instead.
So for example replacing std::iterator_traits<RandomAccessIterator>::value_type with decltype(*it) might well be a win, although auto does often handle such cases.
Subjective judgements enter at the point of "what is difficult", "what is long-winded" and "what is clear", but the rules of procedure can be the same regardless of how you make those judgements in specific cases.
When you want y to always have whatever the declared type of x is.
In the context of your question,
You should use decltype when you want a new variable with precisely the same type as the original variable.
You should use auto when you want to assign the value of some expression to a new variable and you want or need its type to be deduced.
decltype(x) y always declares y with precisely the same type as the type x was declared with. In particular:
If x has type const int then y will have type const int.
If x has type int[100] then y will have type int[100].
If x has type int f(int) then y will have type int f(int). Yes, this actually declares another function with the same type as the original.
If x has type int& then y will have type int&; and if x has type int&& then y will have type int&&.
auto y = x will declare y with the following types, when x has the following types:
If x has type const int, then y will have type int. That is, auto strips top-level cv-qualifiers.
If x has type int[100], then y will have type int*. That is, auto performs array to pointer conversion. [1]
If x has type int f(int), then y will have type int (*)(int). That is, auto performs function to function pointer conversion. [2]
Finally, if x has type int& or int&&, then y will have type int. That is, auto removes references.
[1] You can't use decltype here because you can't copy-initialize an array.
[2] You can't use decltype here because you can't initialize a function.
[3] The reason why auto strips references is that C++ has no expressions of reference type! Once initialized, the "reference ness" of a reference becomes invisible.
Note that decltype also does something entirely different when its argument is not an id-expression, which I won't get into here.
Whenever your variable type isn't related to the expression being evaluated.
E.g:
struct Bar
{
Bar(int) {} // implicitly constructable
}
struct Bar2
{
Bar2(int) {} // implicitly constructable
}
struct Foo
{
static Bar var;
}
struct Foo2
{
static Bar2 var;
}
template <typename T>
void dummy()
{
decltype(T::var) myVar = 42;
}
dummy<Foo>(); // myVar is of type Bar1
dummy<Foo2>(); // myVar is of type Bar2
auto myAutoVar = 42; // type is int
Of course this is just one use case, there are many more out there.
decltype is significantly more versatile that auto and can always be used in place of it. Therefore I think it's pretty safe to say that decltype should only be used in cases where it's completely necessary, so if auto produces the wrong result you should use decltype. Also you can't as of yet use auto in return types and parameters, so you can use decltype there as well. C++14 will significantly increase the potential uses of auto and I would guess c++17 will go further. So the situations to use decltype will only be when you need to change the resulting type of expr
Another thing to consider is that decltype isn't really necessary unless you're writing library code, auto is nice for everyday programming if you want to make your code more concise, it's up for debate wether using as much auto as possible is good, but it's virtually necessary when working with unutterable types like lambdas.

restrict(amp) function type

I can create restrict(amp) function as follows:
auto f = [](int& item) restrict(amp) {item += 1;};
And I can use this function in other restrict(amp) functions, for example:
concurrency::parallel_for_each(av.extent,
[=](concurrency::index<1> idx) restrict(amp)
{
f(av[idx]);
}
);
What type of substituted instead "auto" after compilation? I tried to use the "std::function":
std::function<void (int&) restrict(amp)> f
= [](int& item) restrict(amp) {item += 1;};
but received a compile error.
Thank you for your attention!
The result of a lambda expression is a closure object, and the type of the closure object is unknowable. You can only use auto to declare a variable of its exact type.
However, you can convert a closure object into a suitable instance of an std::function, and if the lambda is non-capturing, you can even convert it to a function pointer. However, this conversion may come at a (significant) cost, so you should prefer using auto as much as possible to handle the actual closure type.
The same goes for bind expressions.
The relevant standard section is 5.1.2(3):
The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type — called the closure type — whose properties are described below. This class type is not an aggregate.
That said, I'm not sure how the special AMP extensions behave in this context, and it's conceivable that AMP-restricted lambdas are not convertible to anything else. I'll try and look this up in the AMP specification.
Update: Sections 2.2.3 and 2.3 of the AMP Specification seem to apply to this question.

C++ auto keyword. Why is it magic?

From all the material I used to learn C++, auto has always been a weird storage duration specifier that didn't serve any purpose. But just recently, I encountered code that used it as a type name in and of itself. Out of curiosity I tried it, and it assumes the type of whatever I happen to assign to it!
Suddenly STL iterators and, well, anything at all that uses templates is 10 fold easier to write. It feels like I'm using a 'fun' language like Python.
Where has this keyword been my whole life? Will you dash my dreams by saying it's exclusive to visual studio or not portable?
auto was a keyword that C++ "inherited" from C that had been there nearly forever, but virtually never used because there were only two possible conditions: either it wasn't allowed, or else it was assumed by default.
The use of auto to mean a deduced type was new with C++11.
At the same time, auto x = initializer deduces the type of x from the type of initializer the same way as template type deduction works for function templates. Consider a function template like this:
template<class T>
int whatever(T t) {
// point A
};
At point A, a type has been assigned to T based on the value passed for the parameter to whatever. When you do auto x = initializer;, the same type deduction is used to determine the type for x from the type of initializer that's used to initialize it.
This means that most of the type deduction mechanics a compiler needs to implement auto were already present and used for templates on any compiler that even sort of attempted to implement C++98/03. As such, adding support for auto was apparently fairly easy for essentially all the compiler teams--it was added quite quickly, and there seem to have been few bugs related to it either.
When this answer was originally written (in 2011, before the ink was dry on the C++ 11 standard) auto was already quite portable. Nowadays, it's thoroughly portable among all the mainstream compilers. The only obvious reasons to avoid it would be if you need to write code that's compatible with a C compiler, or you have a specific need to target some niche compiler that you know doesn't support it (e.g., a few people still write code for MS-DOS using compilers from Borland, Watcom, etc., that haven't seen significant upgrades in decades). If you're using a reasonably current version of any of the mainstream compilers, there's no reason to avoid it at all though.
More recent revisions of the standard have added a few new places that auto can be used. Starting with C++14, you can use auto for the type of a parameter to a lambda:
[](auto s) { return s + 1; }
This does essentially the same thing as the example above--even though it doesn't explicitly use template syntax, this is basically a template that deduces the type of the parameter, and instantiates the template over that type.
That was convenient and useful enough that in C++20, the same capability was added for normal functions, not just lambdas.
But, just as before all of this really comes down to using the same basic type deduction mechanism as we've had for function templates since C++98. auto allows that to be used in more places, and more conveniently, but the underlying heavy lifting remains the same.
It's just taking a generally useless keyword and giving it a new, better functionality. It's standard in C++11, and most C++ compilers with even some C++11 support will support it.
For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For functions, specifies that the return type is a trailing return type or will be deduced from its return statements (since C++14).
Syntax
auto variable initializer (1) (since C++11)
auto function -> return type (2) (since C++11)
auto function (3) (since C++14)
decltype(auto) variable initializer (4) (since C++14)
decltype(auto) function (5) (since C++14)
auto :: (6) (concepts TS)
cv(optional) auto ref(optional) parameter (7) (since C++14)
Explanation
When declaring variables in block scope, in namespace scope, in initialization statements of for loops, etc., the keyword auto may be used as the type specifier.
Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto using the rules for template argument deduction from a function call (see template argument deduction#Other contexts for details). The keyword auto may be accompanied by modifiers, such as const or &, which will participate in the type deduction. For example, given const auto& i = expr;, the type of i is exactly the type of the argument u in an imaginary template template<class U> void f(const U& u) if the function call f(expr) was compiled. Therefore, auto&& may be deduced either as an lvalue reference or rvalue reference according to the initializer, which is used in range-based for loop.
If auto is used to declare multiple variables, the deduced types must match. For example, the declaration auto i = 0, d = 0.0; is ill-formed, while the declaration auto i = 0, *p = &i; is well-formed and the auto is deduced as int.
In a function declaration that uses the trailing return type syntax, the keyword auto does not perform automatic type detection. It only serves as a part of the syntax.
In a function declaration that does not use the trailing return type syntax, the keyword auto indicates that the return type will be deduced from the operand of its return statement using the rules for template argument deduction.
If the declared type of the variable is decltype(auto), the keyword auto is replaced with the expression (or expression list) of its initializer, and the actual type is deduced using the rules for decltype.
If the return type of the function is declared decltype(auto), the keyword auto is replaced with the operand of its return statement, and the actual return type is deduced using the rules for decltype.
A nested-name-specifier of the form auto:: is a placeholder that is replaced by a class or enumeration type following the rules for constrained type placeholder deduction.
A parameter declaration in a lambda expression. (since C++14) A function parameter declaration. (concepts TS)
Notes
Until C++11, auto had the semantic of a storage duration specifier.
Mixing auto variables and functions in one declaration, as in auto f() -> int, i = 0; is not allowed.
For more info : http://en.cppreference.com/w/cpp/language/auto
This functionality hasn't been there your whole life. It's been supported in Visual Studio since the 2010 version. It's a new C++11 feature, so it's not exclusive to Visual Studio and is/will be portable. Most compilers support it already.
The auto keyword is an important and frequently used keyword for C ++.When initializing a variable, auto keyword is used for type inference(also called type deduction).
There are 3 different rules regarding the auto keyword.
First Rule
auto x = expr; ----> No pointer or reference, only variable name. In this case, const and reference are ignored.
int y = 10;
int& r = y;
auto x = r; // The type of variable x is int. (Reference Ignored)
const int y = 10;
auto x = y; // The type of variable x is int. (Const Ignored)
int y = 10;
const int& r = y;
auto x = r; // The type of variable x is int. (Both const and reference Ignored)
const int a[10] = {};
auto x = a; // x is const int *. (Array to pointer conversion)
Note : When the name defined by auto is given a value with the name of a function,
the type inference will be done as a function pointer.
Second Rule
auto& y = expr; or auto* y = expr; ----> Reference or pointer after auto keyword.
Warning : const is not ignored in this rule !!! .
int y = 10;
auto& x = y; // The type of variable x is int&.
Warning : In this rule, array to pointer conversion (array decay) does not occur !!!.
auto& x = "hello"; // The type of variable x is const char [6].
static int x = 10;
auto y = x; // The variable y is not static.Because the static keyword is not a type. specifier
// The type of variable x is int.
Third Rule
auto&& z = expr; ----> This is not a Rvalue reference.
Warning : If the type inference is in question and the && token is used, the names
introduced like this are called "Forwarding Reference" (also called Universal Reference).
auto&& r1 = x; // The type of variable r1 is int&.Because x is Lvalue expression.
auto&& r2 = x+y; // The type of variable r2 is int&&.Because x+y is PRvalue expression.
The auto keyword specifies that the type of the variable that is being declared will be automatically deducted from its initializer. In case of functions, if their return type is auto then that will be evaluated by return type expression at runtime.
It can be very useful when we have to use the iterator. For e.g. for below code we can simply use the "auto" instead of writing the whole iterator syntax .
int main()
{
// Initialize set
set<int> s;
s.insert(1);
s.insert(4);
s.insert(2);
s.insert(5);
s.insert(3);
// iterator pointing to
// position where 2 is
auto pos = s.find(3);
// prints the set elements
cout << "The set elements after 3 are: ";
for (auto it = pos; it != s.end(); it++)
cout << *it << " ";
return 0;
}
This is how we can use "auto" keyword
It's not going anywhere ... it's a new standard C++ feature in the implementation of C++11. That being said, while it's a wonderful tool for simplifying object declarations as well as cleaning up the syntax for certain call-paradigms (i.e., range-based for-loops), don't over-use/abuse it :-)
It's Magic is it's ability to reduce having to write code for every Variable Type passed into specific functions. Consider a Python similar print() function in it's C base.
#include <iostream>
#include <string>
#include <array>
using namespace std;
void print(auto arg) {
cout<<arg<<" ";
}
int main()
{
string f = "String";//tok assigned
int x = 998;
double a = 4.785;
string b = "C++ Auto !";
//In an opt-code ASCII token stream would be iterated from tok's as:
print(a);
print(b);
print(x);
print(f);
}