1

The answers to this question imply that using an initializer list in a loop or with "unknown data" wouldn't work. They don't say why, or how it would fail.

IE, doing this: (this is a nonsense operation, but shows that the contents of the list would change as the loop progresses)

std::vector<float> vec;
// Assume vec is filled with some useful data

for(int i = 0; i < 10; i++) 
{ 
  for(int j = 0; j < 10; j++) 
  {
    for(int k = 0; k < 10; k++) 
    {
      result = std::max({vec[i], vec[j], vec[k]});
      // do something with result...
    }
  }
}

I have code that uses initializer lists to get the max of 3 or more elements very frequently. It seems like things are working, but I am not sure if they are or not.

I'd like to understand if it works. If not, how it fails and why.

I've used a very heavy set of warnings, nothing has ever reported "warning: may be using initializer list incorrectly" or so-on.

7
  • 3
    I'm not sure where in the linked question it seems to imply it wouldn't work. This code is fine as long as the indices are in bounds. Commented Nov 15, 2019 at 22:31
  • Yuushi's answer states: "If the data changes at runtime, store it in a container and use max_element". The second answer also states right away "Assuming you have an unknown set of data, you can always place it in an array / vector / list and run..." Both imply that if the data changes, it won't work. Was I just misreading it? Commented Nov 15, 2019 at 22:33
  • What they are saying is that if you need to get the data at run time then you need to use some other container. Techinally you're not getting the data at run time because you are coding std::max({vec[i], vec[j], vec[k]}) into the program. You can't use a std::initializer_list to get data from a user, but you can build one from data from the user after you have it. Make sense? Commented Nov 15, 2019 at 22:35
  • 2
    vectors,arrays, ect are writable. You can add data to them. std::initiliazer_list is not. Once you create one, its size and the value of the elements inside it are constant. This is why you can't use it to get input. You can only create one and pass it around to be read from. Commented Nov 15, 2019 at 22:45
  • 3
    @TylerShellberg In the linked question, they say to use max_element when the number of element changes. For example, if you don't know if you will have 3 or 4 values to put in the initializer list, then you cannot use max. In your case, you seem to know that it will always be 3 elements, which works with max even if the value of the 3 elements changes at runtime. Commented Nov 15, 2019 at 22:46

1 Answer 1

2

The answers to this question imply that using an initializer list in a loop or with "unknown data" wouldn't work.

This is a misinterpretation of what was said, due to an omitted word. The statement you're talking about was in response to this comment:

How would you make this work if the data is generated in a loop, or the number of data changes at runtime?

Emphasis added. So when the person says:

If the data changes at runtime, ...

The response simply omitted the "number" part, since it's a direct response to the previous comment.

Braced-init-lists can contain arbitrary numbers of items, but the number of items in the list must be defined at compile-time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.