tags:

views:

120

answers:

3
+2  Q: 

List initializer

I am trying to declare an iterator of type std::initializer_list as it is described in this answer.

But I always get this error:

error: 'iterator' is not a member of 'std::initializer_list<int>'

May someone guide me how to declare an iterator of type std::initializer_list?

EDIT:

This code for example will not work. In addition, The compiler that I use supports the new standard draft

int findCommon(std::initializer_list<int> nums)
{
    std::initializer_list<int>::iterator it;
    for (it = nums.begin() ; it != nums.end() ; ++it)
    {
        std::cout << *it << std::endl;  
    }
    return 1;
}
+1  A: 

You need to add

#include <initializer_list>

and for this example probably also

#include <iostream>

Also make sure you are using the -std=c++0x switch to g++. And you need at least g++ version 4.4. When using the compiler from MacPorts, the compiler is called g++-mp-4.5 or g++-mp-4.4. (Incorrect assumption).

I noticed some recently fixed bugs related to initializer_list on the GCC bugzilla, so I recommend getting the newest version of GCC, 4.5.1. Because when using this version I am able to compile both your examples without problems.

schot
it doesn't help. I can initialize data structure from the type std::initializer_list<int> and it works well. But I just can't declare the iterator for it
s11
@sami, on my Mac, with g++ from MacPorts, your code compiles when I add the two #includes. Can you post the exact command line you use for compiling?
schot
As I said, I don't have problem by using the the new draft. I can use the variadic template and initialize_list. The only problem is by declaring the iterator from the type std::initialize_list. the command that I use is g++ -std=c++0x example.cpp -o example
s11
why I can declare a data structure like std::initializer_list<int> Nr; but I cann't declare an iterator for it like std::initializer_list<int>::iterator it;
s11
@sami, have you tried `g++-mp-4.5` instead of `g++`? I can't see any other problem.
schot
You might also want to check out the sourceof the initializer_list, if there really is an iterator defined.
Ronny
+1  A: 

Your compiler's implementation probably just doesn't have these typedefs (e.g with GCC 4.4.1, initializer_list doesn't contain any typedefs, you can check the header yourself).

As a "workaround", use auto:

int findCommon(std::initializer_list<int> nums)
{
    for (auto it = nums.begin() ; it != nums.end() ; ++it)
    {
        std::cout << *it << std::endl;
    }
    return 1;
}

Or rely on initializer_list<T>::iterator being a synonym for const T*.

UncleBens
A: 

You can avoid directly specifying the iterator type, which should lead to cleaner code:

void f(std::initializer_list<int> nums)
{
  for (auto x : nums) {
    std::cout << x << '\n';
  }
  // or:
  std::copy(begin(nums), end(nums), std::ostream_iterator(std::cout, "\n"));
}

Range-based for loops are supported in GCC 4.6. (Any problem calling them foreach loops, informally?)

Including <iterator> may be required for std::begin and std::end, though several headers guarantee they are declared (24.6.5, N3092).

Roger Pate