How to use structured binding in an array passed as arg to some function?Using std NamespaceIs there a max array length limit in C++?Autocompletion in VimFunction passed as template argumentWhat is the copy-and-swap idiom?How do I use arrays in C++?Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognitionc++ passing Pointers into a function, for passing int arrays out of and into a functionReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationspassing character arrays to a functionStructure Binding : binding to public data members (inherited base class )

Rushed passport - does my reason qualify?

Square spiral in Mathematica

Can a person still be an Orthodox Jew and believe that the Torah contains narratives that are not scientifically correct?

What are the effects of eating many berries from the Goodberry spell per day?

How come Arya Stark didn't burn in Game of Thrones Season 8 Episode 5

Given 0s on Assignments with suspected and dismissed cheating?

Why does the U.S military use mercenaries?

Canadian citizen who is presently in litigation with a US-based company

Why does string strummed with finger sound different from the one strummed with pick?

Pedaling at different gear ratios on flat terrain: what's the point?

Can I pay my credit card?

How to handle professionally if colleagues has referred his relative and asking to take easy while taking interview

Why doesn't Iron Man's action affect this person in Endgame?

What color to choose as "danger" if the main color of my app is red

Why is so much ransomware breakable?

He is the first man to arrive here

How can we delete item permanently without storing in Recycle Bin?

Usage of the relative pronoun "dont"

Why would you put your input amplifier in front of your filtering for and ECG signal?

Does a non-singular matrix have a large minor with disjoint rows and columns and full rank?

How to continually and organically let my readers know what time it is in my story?

Why would company (decision makers) wait for someone to retire, rather than lay them off, when their role is no longer needed?

Capital gains on stocks sold to take initial investment off the table

How could it be that 80% of townspeople were farmers during the Edo period in Japan?



How to use structured binding in an array passed as arg to some function?


Using std NamespaceIs there a max array length limit in C++?Autocompletion in VimFunction passed as template argumentWhat is the copy-and-swap idiom?How do I use arrays in C++?Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognitionc++ passing Pointers into a function, for passing int arrays out of and into a functionReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationspassing character arrays to a functionStructure Binding : binding to public data members (inherited base class )






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








8















I'm trying to decompose an array of 2 integers given to a function into x, y



It doesn't work when using int init[2] as a parameter. But it does when I change it to int (&init)[2].



vector<vector<State>> Search(vector<vector<State>> board,
int init[2], int goal[2])
auto [x, y] = init;



What does (&init) mean here? And why it doesn't work when using int init[2]?










share|improve this question
























  • Don't do using namespace std;.

    – Williham Totland
    May 11 at 19:50


















8















I'm trying to decompose an array of 2 integers given to a function into x, y



It doesn't work when using int init[2] as a parameter. But it does when I change it to int (&init)[2].



vector<vector<State>> Search(vector<vector<State>> board,
int init[2], int goal[2])
auto [x, y] = init;



What does (&init) mean here? And why it doesn't work when using int init[2]?










share|improve this question
























  • Don't do using namespace std;.

    – Williham Totland
    May 11 at 19:50














8












8








8


0






I'm trying to decompose an array of 2 integers given to a function into x, y



It doesn't work when using int init[2] as a parameter. But it does when I change it to int (&init)[2].



vector<vector<State>> Search(vector<vector<State>> board,
int init[2], int goal[2])
auto [x, y] = init;



What does (&init) mean here? And why it doesn't work when using int init[2]?










share|improve this question
















I'm trying to decompose an array of 2 integers given to a function into x, y



It doesn't work when using int init[2] as a parameter. But it does when I change it to int (&init)[2].



vector<vector<State>> Search(vector<vector<State>> board,
int init[2], int goal[2])
auto [x, y] = init;



What does (&init) mean here? And why it doesn't work when using int init[2]?







c++ c++17






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 11 at 17:35









StoryTeller

108k16227290




108k16227290










asked May 11 at 17:10









BrunoBruno

14414




14414












  • Don't do using namespace std;.

    – Williham Totland
    May 11 at 19:50


















  • Don't do using namespace std;.

    – Williham Totland
    May 11 at 19:50

















Don't do using namespace std;.

– Williham Totland
May 11 at 19:50






Don't do using namespace std;.

– Williham Totland
May 11 at 19:50













1 Answer
1






active

oldest

votes


















8














int (&init)[2] is a reference to an array of two integers. int init[2] as a function parameter is a leftover from C++'s C heritage. It doesn't declare the function as taking an array. The type of the parameter is adjusted to int* and all size information for an array being passed into the function is lost.



A function taking int init[2] can be called with an array of any size, on account of actually taking a pointer. It may even be passed nullptr. While a function taking int(&)[2] may only be given a valid array of two as an argument.



Since in the working version init refers to a int[2] object, structured bindings can work with that array object. But a decayed pointer cannot be the subject of structured bindings, because the static type information available only gives access to a single element being pointed at.






share|improve this answer


















  • 1





    Thanks!! I got it :)

    – Bruno
    May 11 at 17:25











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56092567%2fhow-to-use-structured-binding-in-an-array-passed-as-arg-to-some-function%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









8














int (&init)[2] is a reference to an array of two integers. int init[2] as a function parameter is a leftover from C++'s C heritage. It doesn't declare the function as taking an array. The type of the parameter is adjusted to int* and all size information for an array being passed into the function is lost.



A function taking int init[2] can be called with an array of any size, on account of actually taking a pointer. It may even be passed nullptr. While a function taking int(&)[2] may only be given a valid array of two as an argument.



Since in the working version init refers to a int[2] object, structured bindings can work with that array object. But a decayed pointer cannot be the subject of structured bindings, because the static type information available only gives access to a single element being pointed at.






share|improve this answer


















  • 1





    Thanks!! I got it :)

    – Bruno
    May 11 at 17:25















8














int (&init)[2] is a reference to an array of two integers. int init[2] as a function parameter is a leftover from C++'s C heritage. It doesn't declare the function as taking an array. The type of the parameter is adjusted to int* and all size information for an array being passed into the function is lost.



A function taking int init[2] can be called with an array of any size, on account of actually taking a pointer. It may even be passed nullptr. While a function taking int(&)[2] may only be given a valid array of two as an argument.



Since in the working version init refers to a int[2] object, structured bindings can work with that array object. But a decayed pointer cannot be the subject of structured bindings, because the static type information available only gives access to a single element being pointed at.






share|improve this answer


















  • 1





    Thanks!! I got it :)

    – Bruno
    May 11 at 17:25













8












8








8







int (&init)[2] is a reference to an array of two integers. int init[2] as a function parameter is a leftover from C++'s C heritage. It doesn't declare the function as taking an array. The type of the parameter is adjusted to int* and all size information for an array being passed into the function is lost.



A function taking int init[2] can be called with an array of any size, on account of actually taking a pointer. It may even be passed nullptr. While a function taking int(&)[2] may only be given a valid array of two as an argument.



Since in the working version init refers to a int[2] object, structured bindings can work with that array object. But a decayed pointer cannot be the subject of structured bindings, because the static type information available only gives access to a single element being pointed at.






share|improve this answer













int (&init)[2] is a reference to an array of two integers. int init[2] as a function parameter is a leftover from C++'s C heritage. It doesn't declare the function as taking an array. The type of the parameter is adjusted to int* and all size information for an array being passed into the function is lost.



A function taking int init[2] can be called with an array of any size, on account of actually taking a pointer. It may even be passed nullptr. While a function taking int(&)[2] may only be given a valid array of two as an argument.



Since in the working version init refers to a int[2] object, structured bindings can work with that array object. But a decayed pointer cannot be the subject of structured bindings, because the static type information available only gives access to a single element being pointed at.







share|improve this answer












share|improve this answer



share|improve this answer










answered May 11 at 17:16









StoryTellerStoryTeller

108k16227290




108k16227290







  • 1





    Thanks!! I got it :)

    – Bruno
    May 11 at 17:25












  • 1





    Thanks!! I got it :)

    – Bruno
    May 11 at 17:25







1




1





Thanks!! I got it :)

– Bruno
May 11 at 17:25





Thanks!! I got it :)

– Bruno
May 11 at 17:25



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56092567%2fhow-to-use-structured-binding-in-an-array-passed-as-arg-to-some-function%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?