Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Dransparency
Jul 19, 2006
I'm having some trouble getting standard iterators to work, and google isn't giving me much help.

Currently I have something like this:

code:
#include <vector>

template<typename itemType>
class Foo
{
   typedef std::iterator<std::forward_iterator_tag, itemType> Iterator;
   bool exists(Iterator& beg, Iterator& end) const;
};

int main()
{
   std::vector<int> vals;
   //fill with values
   Foo<int> test;
   test.exists(vals.begin(), vals.end());
}
I got the typedef off of some sites I found, but it's difficult to find documentation for the std::iterator class itself.

When I compile (using VS2008) I get an error saying:
code:
'...cannot convert parameter 1 from 'std::_Vector_iterator<_Ty,_Alloc>' to 'std::iterator<_Category,_Ty> &'
This suggests to me that either the iterators used by the STL aren't derived from the std::iterator class or I'm using the wrong template arguments for std::iterator.

Is there another class I should be deriving from or different template arguments I should be providing to std::iterator? The only requirements I have for the iterator are that it can advance forward and check for equality with other iterators (as well as access whatever it points to, obviously).

I've noticed that the functions in the <algorithms> header just use templates that take any old class as an iterator, leaving it up to the user to make sure it supplies the proper operations. I would assume that is because they need to support pointers as well as actual iterator classes. Even though I don't need the support for pointers, would this be a better approach for my problem? I've been avoiding it so far because I want to make this as standardized as possible, plus I don't know the syntax for a member function with additional template parameters from its class. Can I just use nested templates?

Adbot
ADBOT LOVES YOU

Dransparency
Jul 19, 2006

floWenoL posted:

What do you guys think of this?

Personally I think this is disgusting:

code:
// When it requires multiple lines, indent 4 spaces, putting the colon on
// the first initializer line:
MyClass::MyClass(int var)
    : some_var_(var),             // 4 space indent
      some_other_var_(var + 1) {  // lined up
  ...
  DoSomething();
  ...
}
What's the logic behind the colon getting a newline but the opening brace not? I'm not trying to start a brace style war, but if you've elected to use K&R for braces, why not use something similar for colons?

The indentation also bothers me. I would much rather have lines move to the right the farther I read into the function, rather than the weird shifting going on here.


I also don't like the bit on function naming. It seems like it could introduce inconsistencies if there isn't a direct relationship between public characteristics and internal implementation variables.



Edit: I'd be interested in hearing what the rest of you think about the section on streams in particular. Not just for Google's case (where they need to maintain consistency with old code) but for general practice programming. The guide seems to dismiss the issue as "they both have pros and cons, so either one is just as good."

Dransparency fucked around with this message at 04:35 on Jun 30, 2008

Dransparency
Jul 19, 2006

That Turkey Story posted:

Edit: I don't know why you're getting all upset about naming and indentation, etc. You can't really fault anyone for something that's nearly entirely a subjective choice.

Well, he asked for our opinions, so I gave mine. I really do think having a different naming scheme for functions that deal with member variables is bad, since it ties interface to implementation.

Dransparency
Jul 19, 2006

Nutsack Rangoon posted:

Let me preface by saying I'm terrible:


This isn't compiling. Can someone tell me why? The compiler is telling me something about not expecting "<" before the for loop?

You've mistyped "<=" as "=<".

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply