Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does returning a vector initialized with curly braces within normal brackets cause a compilation error?

Tags:

c++

c++11

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
like image 460
sol Avatar asked Nov 02 '21 08:11

sol


People also ask

What is the purpose of curly brackets {}?

In writing, curly brackets or braces are used to indicate that certain words and/or sentences should be looked at as a group.

What does curly brackets mean in code?

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.

What does curly brackets mean in R?

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.

What does curly bracket mean in HTML?

It does nothing in HTML.

When to use brace initialization in C++?

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.

How do I use brace initialization in Python?

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 };

How do you pass objects between braces in a function?

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.

Why are there braces in STL containers?

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!"};


2 Answers

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.

like image 166
Jarod42 Avatar answered Oct 23 '22 20:10

Jarod42


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.

like image 1
justANewb stands with Ukraine Avatar answered Oct 23 '22 18:10

justANewb stands with Ukraine