I just wrote a simple method to return a vector made with two int arguments. However, when I return the initialized int vector within normal brackets, it causes compilation error.
std::vector<int> getVec(int x, int y)
{
return({x, y}); // This causes compile error
return {x, y}; // This is fine
}
The error message says:
q.cpp: In function ‘std::vector<int> getVec(int, int)’:
q.cpp:8:21: error: expected ‘;’ before ‘}’ token
8 | return({x, y});
| ^
| ;
q.cpp:8:15: error: could not convert ‘((void)0, y)’ from ‘int’ to ‘std::vector<int>’
8 | return({x, y});
| ^~~~~~~~
| |
| int
In writing, curly brackets or braces are used to indicate that certain words and/or sentences should be looked at as a group.
In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true.
According to the R Language Definition, the difference between a curly brace and parentheses is that the curly brace yields a function object, whereas the parentheses give you an expression object; it should be surprising that there might be a slight speed difference between these two things.
It does nothing in HTML.
You can use brace initialization anywhere you would typically do initialization—for example, as a function parameter or a return value, or with the new keyword: In /std:c++17 mode, the rules for empty brace initialization are slightly more restrictive. See Derived constructors and extended aggregate initialization.
You can use brace initialization anywhere you would typically do initialization—for example, as a function parameter or a return value, or with the newkeyword: class_d* cf = new class_d{4.5}; kr->add_d({ 4.5 }); return { 4.5 };
Instead, we can directly pass in a set of objects between braces as an argument to this function. For example, with this calling code: This relies on the fact that the constructor of std::vector that takes a std::initialiser_list is not explicit. Therefore, the function calls makes an implicit construction of the vector from the initializer_list.
This doesn’t just apply to STL containers. The braces syntax allows to intialize the standard collections that can carry different types, that is to say std::tuple and std::pair: std::pair answer = {"forty-two", 42}; std::tuple cue = {3, 2, 1, "go!"};
From return statement:
- return expression(optional) ; (1)
- return braced-init-list ; (2)
Remember the {..}
is not an expression, has no type. There exist some contexts which allow {..}
to be deduced in some type.
There is a special case for return {..}
(2) and uses copy-list-initialization to construct the return value of the function.
In return ({x, y})
, we go in (1), and {x, y}
still has no type, no special cases for ({..})
. So the error.
return {x, y};
{x, y}
is a std::initializer_list<int>
, so when your compiler sees this, it can deduce the type into:
std::vector<int>{std::initializer_list<int>{x, y}};
But:
return ({x, y});
This isn't systax correct:
std::vector<int>({std::initializer_list<int>{x, y}});
The compiler doesn't know hơ to deduce this, so it gives a compilation error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With