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 )

Is "cool" appropriate or offensive to use in IMs?

Parallel fifths in the orchestra

Which European Languages are not Indo-European?

Convert Byte array into collection of items of different types

Is the Unsullied name meant to be ironic? How did it come to be?

Why does the hash of infinity have the digits of π?

Do I need full recovery mode when I have multiple daily backup?

Website returning plaintext password

Where have Brexit voters gone?

Does this strict reading of the rules allow both Extra Attack and the Thirsting Blade warlock invocation to be used together?

Why does this if-statement combining assignment and an equality check return true?

bash regexp matching fails in [[ ]]

What happened to boiled-off gases from the storage tanks at Launch Complex 39?

NIntegrate doesn't evaluate

How did NASA Langley end up with the first 737?

Can my floppy disk still work without a shutter spring?

How to politely tell someone they did not hit "reply to all" in an email?

Find the three digit Prime number P from the given unusual relationships

Is it truly impossible to tell what a CPU is doing?

How to let other coworkers know that I don't share my coworker's political views?

How to cut a climbing rope?

Why are GND pads often only connected by four traces?

Is Jon Snow the last of his House?

How to reverse input order?



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

109k16229293




109k16229293










asked May 11 at 17:10









BrunoBruno

14315




14315












  • 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

109k16229293




109k16229293







  • 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

How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?