-2

p is a list of integers.

std::list<int> p;
    if ( 2 % p(0) == 0 );

But p has "expression must have integral or unscoped enum type" error.

Why?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • What are you trying to do? If you want to know if the first item of the list is even, then Dukeling's answer will handle it. If not, then you need to explain more. – Marshall Clow Feb 25 '13 at 16:46

1 Answers1

2

list doesn't overload operator(int), this is a requirement for you to be able to say p(0).

If you meant p[0], list doesn't overload operator[int] either, this is only for vector, map (or actually operator[keyType]), etc. This is because lists don't have random access (meaning you can't get any element, unless you loop through)

You can however do something like:

if (2 % p.front() == 0)

or

if (2 % *p.begin() == 0)

which accesses the first element.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138